#!/bin/bash (magic line added for syntax highlighting only) # Usage: Path_Contains # Returns 0 (success) if is a substring of $ function Path_Contains() { local path=":`eval echo '$'$1`:" shift [ "$path" = "::" ] && return 1 # empty path while [ $# -gt 0 ] do [ "${path##*:$1:*}" ] && return 2 # not found shift done } # Usage: Append_To_Path # Add to $ if not already present in its colon-separated # list of entries. Adds to the end of the list. function Append_To_Path() { local name="$1" term shift while [ $# -gt 0 ] do eval term=$1 # tilde expansion Path_Contains "$name" "$term" || { eval "${name}="'${'"${name}%:"'}' # remove trailing colon eval "${name}=\$${name}:${term}" # assign } shift done eval "${name}="'${'"${name}#:"'}' # remove starting colon } # Usage: Append_To_Path # Add to $ if not already present in its colon-separated # list of entries. Adds to the beginning of the list. function Prepend_To_Path() { local name="$1" term shift while [ $# -gt 0 ] do eval term=$1 # tilde expansion Path_Contains "$name" "$term" || { eval "${name}="'${'"${name}#:"'}' # remove starting colon eval "${name}=${term}:\$${name}" # assign } shift done eval "${name}="'${'"${name}%:"'}' # remove trailing colon }