In my .bash_profile I have the following lines:
PATHDIRS=\"
/usr/local/mysql/bin
/usr/local/share/python
/opt/local/bin
/opt/local/sbin
$HOME/bin\"
for
Unlike other shells, zsh does not perform word splitting or globbing after variable substitution. Thus $PATHDIRS
expands to a single string containing exactly the value of the variable, and not to a list of strings containing each separate whitespace-delimited piece of the value.
Using an array is the best way to express this (not only in zsh, but also in ksh and bash).
pathdirs=(
/usr/local/mysql/bin
…
~/bin
)
for dir in $pathdirs; do
if [ -d $dir ]; then
path+=$dir
fi
done
Since you probably aren't going to refer to pathdirs
later, you might as well write it inline:
for dir in \
/usr/local/mysql/bin \
… \
~/bin
; do
if [[ -d $dir ]]; then path+=$dir; fi
done
There's even a shorter way to express this: add all the directories you like to the path
array, then select the ones that exist.
path+=/usr/local/mysql/bin
…
path=($^path(N))
The N
glob qualifier selects only the matches that exist. Add the -/
to the qualifier list (i.e. (-/N)
or (N-/)
) if you're worried that one of the elements may be something other than a directory or a symbolic link to one (e.g. a broken symlink). The ^
parameter expansion flag ensures that the glob qualifier applies to each array element separately.
You can also use the N
qualifier to add an element only if it exists. Note that you need globbing to happen, so path+=/usr/local/mysql/bin(N)
wouldn't work.
path+=(/usr/local/bin/mysql/bin(N-/))