Issue
Got the function inside my ~/.zshrc file, which prints command not found: adb
install_apk_on_device() {
set -x
local device=${1}
local path=${2}
if [[ -z "${device}" ]]; then
echo "The device is empty"
elif [[ -z "${path}" ]]; then
echo "The path is empty"
else
adb -s "$device" install -r "$path"
fi
}
output:
+install_apk_on_device:3> local device=emulator-5554
+install_apk_on_device:4> local path=MY_APK_PATH
+install_apk_on_device:6> [[ -z emulator-5554 ]]
+install_apk_on_device:8> [[ -z MY_APK_PATH ]]
+install_apk_on_device:11> adb -s emulator-5554 install -r MY_APK_PATH
install_apk_on_device:11: command not found: adb
However, if you run this adb command directly with the same arguments, like adb -s 52036d8b50671495 install -r .//androidApp/build/outputs/apk/debug/androidApp-debug.apk
, the apk will be installed
Also, if I take the adb command out of the else-branch right to the begging of the function, it works as expected
install_apk_on_device() {
adb -s "$1" install -r "$2"
local device=${1}
local path=${2}
if [[ -z "${device}" ]]; then
echo "The device is empty"
elif [[ -z "${path}" ]]; then
echo "The path is empty"
fi
}
These are my PATHS:
PATH=/bin:/usr/bin:/usr/local/bin:${PATH}
export PATH
export HOME=/Users/ruanvd5
export JAVA_HOME=$HOME/Library/Java/JavaVirtualMachines/corretto-1.8.0_275/Contents/Home
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
export PATH=$PATH:$HOME/Programming/elixirSourceCode/elixir/bin
export GRADLE_USER_HOME=$HOME/.gradle
Also, this is my declare -p PATH
export -T PATH path=(
/bin /usr/bin /usr/local/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /usr/local/munki /Library/Apple/usr/bin
/Users/ruanvd5/Library/Android/sdk/emulator
/Users/ruanvd5/Library/Android/sdk/tools
/Users/ruanvd5/Library/Android/sdk/tools/bin
/Users/ruanvd5/Library/Android/sdk/platform-tools
/Users/ruanvd5/Programming/elixirSourceCode/elixir/bin
)
Confused what I wrote wrong :-)
Solution
Unlike bash (and other POSIX-compliant shells; this is a place where zsh breaks the specification, as the standard specifies that only all-caps names in the namespace used for environment variables -- which is also the namespace used for regular shell variables -- should be meaningful to POSIX-specified tools), the name path
is meaningful to zsh even in lowercase. Change your variable name.
That is to say, instead of local path=${2}
(which, in zsh, changes the active PATH
as a side effect), make it local apk_path=$2
.
install_apk_on_device() {
local device apk_path
device=$1; apk_path=$2
[[ -z $device ]] && { echo "The device is empty" >&2; return 1; }
[[ -z $apk_path ]] && { echo "The path is empty" >&2; return 1; }
adb -s "$device" install -r "$apk_path"
}
Answered By - Charles Duffy