Solution
if [[ ! "$PATH" == *your_path* ]]; then
PATH="${PATH:+${PATH}:}your_path"
fi
Explanation
[[ ! "$PATH" == *your_path* ]]
checks if PATH
contains your new path already, hence avoiding duplicates. This doesn’t work well for nested bin directories, but that rarely happens.
${parameter:+word}
uses word
if parameter
is null or unset. Therefore, if PATH
is unset or empty, the new PATH
contains only your_path
and doesn’t contain a leading :
.
References
- bash(1)