3 ms·
What if the entry is already present, but at the end or the beginning?
by nerdponx 5y ago
What if the entry is already present, but at the end or the beginning?
- bewuethr 5y agoI 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.
- hvdijk 5y agoThat's what the extra :s in ":$PATH:" are for: they prepend and append an empty component to the list. Any component in the original "$PATH" will still be in ":$PATH:" too, but no longer be at the end or the beginning, allowing the pattern to be simplified.