Ad
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.

Code
Diff
  • 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=$IFS
    • IFS=':'
    • t=(${PATH})
    • unset IFS
    • t=("$1" ${t[@]%%"$1"})
    • # output the new array
    • IFS=':'
    • echo -n "${t[*]}"
    • unset t
    • IFS=$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"