case ":$PATH:" in
*:$1:*) return 1 ;;
*) return 0 ;;
esac
? case ":$PATH:" in
*:$1:*) return 1 ;;
*) return 0 ;;
esac
? 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.