3 ms·
I use something like this: for dir in /path/to/dir1 /path/to/dir2; do case :${PATH:=$dir}: in *:"$dir":*) ;; *) PATH=$dir:$PATH ;;
by bewuethr 5y ago
I use something like this:
for dir in /path/to/dir1 /path/to/dir2; do
case :${PATH:=$dir}: in
*:"$dir":*) ;;
*) PATH=$dir:$PATH ;;
esac
done
If PATH isn't set, ${PATH:=$dir} sets it to $dir.
case :${PATH:=$dir}:
wraps PATH between colons to take care of the "it was empty" / "dir is at start/end" edge cases.
The first case is hit when the PATH contained the directory already (no-op); the second case prepends the new directory to the PATH.
- hvdijk 5y agoBeware that PATH being unset or empty is a special case and does not necessarily mean that no directories are searched. Different shells handle it differently, and other utilities spawned by the shells handle it themselves in a way that may or may not match the shell. Whether setting PATH to $dir if it was previously unset or empty is the right thing to do is therefore a matter of opinion. Personally, I would stay out of that mess and either issue an error or leave PATH unmodified.