Trevor Sears Trevor Sears - 4 years ago 93
Bash Question

zsh Looping through multiple parameters

In my old

.bashrc
, I had a short section as follows:

PATH2ADD_SCRIPTBIN="/home/foo/bar/scriptbin"
PATH2ADD_PYTHONSTUFF="/home/foo/bar/pythonprojects"

PATH2ADDLIST="$PATH2ADD_SCRIPTBIN $PATH2ADD_PYTHONSTUFF"

for PATH2ADD in $PATH2ADDLIST; do

if [ -z `echo $PATH | grep "$PATH2ADD"` ]; then

export PATH=$PATH:$PATH2ADD
echo "Added '$PATH2ADD' to the PATH."

fi

done


And in Bash, this worked just as intended: it appended the paths I included in
$PATH2ADDLIST
if they were not already present in the path (I had to do this after realizing how huge my path was getting each time I was sourcing my
.bashrc
). The output (when the provided paths were not already present) was as follows:

Added '/home/foo/bar/scriptbin' to the PATH.
Added '/home/foo/bar/pythonprojects' to the PATH.


However, I recently switched over to the magical land of
Zsh
, and the exact same lines of text now produce this result:

Added '/home/foo/bar/scriptbin /home/foo/bar/pythonprojects' to the PATH.


Now I'm pretty sure that this is because of some difference in how
Zsh
does parameter expansion, or that it has something to do with how
Zsh
changes the
for
loop, but I'm not really sure how to fix this.

Might anyone have some insight?

Answer Source

Use an array to store those variables, i.e.

PATH2ADD_SCRIPTBIN="/home/foo/bar/scriptbin"
PATH2ADD_PYTHONSTUFF="/home/foo/bar/pythonprojects"

# Initializing 'PATH2ADDLIST' as an array with the 2 variables
# to make the looping easier

PATH2ADDLIST=("${PATH2ADD_SCRIPTBIN}" "${PATH2ADD_PYTHONSTUFF}")

# Looping through the array contents
for PATH2ADD in "${PATH2ADDLIST[@]}"
do 
    # Using the exit code of 'grep' directly with a '!' negate
    # condition
    if ! echo "$PATH" |  grep -q "$PATH2ADD"
    then
        export PATH=$PATH:$PATH2ADD
        echo "Added '$PATH2ADD' to the PATH."
    fi
done

This way it makes it more compatible in both zsh and bash. A sample dry run on both the shells,

# With interpreter set to /bin/zsh

zsh script.sh 
Added '/home/foo/bar/scriptbin' to the PATH.
Added '/home/foo/bar/pythonprojects' to the PATH.

and in bash

bash script.sh 
Added '/home/foo/bar/scriptbin' to the PATH.
Added '/home/foo/bar/pythonprojects' to the PATH.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download