Bash
A more succinct version of the function but does require dependencies on tr, sed, and awk. I bet it could be reworked to remove tr and either sed or awk.
add_PATH() { t=$(echo $PATH | tr : '\n' | awk '!x[$0]++' | tr '\n' : | sed 's/.$//') echo -n "$t" unset t } echo "Original Path: $PATH" export PATH=$(add_PATH /usr/local/bin) echo "New Path: $PATH"
- add_PATH() {
oIFS=$IFSIFS=':'t=(${PATH})unset IFSt=("$1" ${t[@]%%"$1"})# output the new arrayIFS=':'echo -n "${t[*]}"unset tIFS=$oIFS- t=$(echo $PATH | tr : '\n' | awk '!x[$0]++' | tr '\n' : | sed 's/.$//')
- echo -n "$t"
- unset t
- }
- echo "Original Path: $PATH"
- export PATH=$(add_PATH /usr/local/bin)
- echo "New Path: $PATH"