add2path
# version 1 function add2path() { declare defaultpath path pathvar # add the new path at the beginning of $PATHVARIABLE if [[ "${1}" == '-b' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi path="${3}:${2}" #path="${path// /}" # remove spaces printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' # add the new path at the end of $PATHVARIABLE elif [[ "${1}" == '-e' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary pathvar="$(printf "%s" "${2//:/$'\n'}" | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')" path="${pathvar}:${3}" path="${path//::/:}" #path="${path// /}" # remove spaces printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' # default: add the new path at the beginning of $PATHVARIABLE elif [[ $# -eq 2 ]]; then if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi path="${2}:${1}" #path="${path// /}" # remove spaces printf "%s" "${path//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' else defaultpath=' /usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/include /usr/local/man /opt/local/bin /opt/local/sbin /opt/local/lib /opt/local/include /opt/local/man /usr/X11R6 /usr/X11R6/bin /usr/X11R6/include /usr/X11R6/lib /usr/X11R6/man ' defaultpath="${defaultpath// /}" # remove spaces defaultpath="${defaultpath//$'\n'/:}" # convert newlines into colons defaultpath="${defaultpath#:}" # cut off leading colon defaultpath="${defaultpath%:}" # cut off trailing colon printf "%s" "${defaultpath}" return 1 fi return 0 } # usage: # add2path [$PATHVARIABLE] /path/to/dir # add2path [-b|-e] [$PATHVARIABLE] /path/to/dir # store the original $PATH OPATH="${PATH}" # create test directories testdir="${HOME}/Desktop/testdir" /bin/mkdir -p "${testdir}" for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; } # list test directories echo "${testdir}" for ((i=0;i<=5;i++)) { echo "${testdir}${i}"; } add2path "${PATH}" "${testdir}" | tr ':' '\n' | nl printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="$(add2path "${PATH}" "${testdir}")" printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}")"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl add2path | tr ':' '\n' | nl add2path abc | tr ':' '\n' | nl add2path -b "${PATH}" "${testdir}" | tr ':' '\n' | nl add2path -e "${PATH}" "${testdir}" | tr ':' '\n' | nl # restore original $PATH export PATH="${OPATH}" printf "%s\n" "${PATH}" | tr ':' '\n' | nl #-------------------------------------------------------------- # version 2 function add2path() { # add the new path at the beginning of $PATHVARIABLE if [[ "${1}" == '-b' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi printf "%s" "${3}:${2}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \ /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' # add the new path at the end of $PATHVARIABLE elif [[ "${1}" == '-e' ]]; then if [[ ! -d "${3}" ]]; then printf "%s" "${2}"; return 1; fi # first remove identical path from $PATHVARIABLE (path identical with ${3}) if necessary pathvar="$(printf "%s" "${2}" | /usr/bin/tr ':' '\n' | /usr/bin/egrep -v "^${3}$" | /usr/bin/tr '\n' ':')" printf "%s" "${pathvar}:${3}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \ /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' # default: add the new path at the beginning of $PATHVARIABLE elif [[ $# -eq 2 ]]; then if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi printf "%s" "${2}:${1}" | /usr/bin/sed -E -e '/^:+/s/^:+//' -e '/:+$/s/:+$//' -e '/::+/s/::+/:/g' | /usr/bin/tr ':' '\n' | \ /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' | /usr/bin/sed -E -e '/:+$/s/:+$//' else path=' /usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/include /usr/local/man /opt/local/bin /opt/local/sbin /opt/local/lib /opt/local/include /opt/local/man /usr/X11R6 /usr/X11R6/bin /usr/X11R6/include /usr/X11R6/lib /usr/X11R6/man ' printf "%s" "${path// /}" | /usr/bin/tr '\n' ':' | /usr/bin/sed -E '/^:|:$/s/^:|:$//g' return 1 fi return 0 } # usage: # add2path [$PATHVARIABLE] /path/to/dir # add2path [-b|-e] [$PATHVARIABLE] /path/to/dir OPATH="${PATH}" testdir="${HOME}/Desktop/testdir" /bin/mkdir -p "${testdir}" for ((i=0;i<=5;i++)) { /bin/mkdir -p "${testdir}${i}"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="$(add2path "${PATH}" "${testdir}" )" printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -b "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl for ((i=0;i<=5;i++)) { export PATH="$(add2path -e "${PATH}" "${testdir}${i}" )"; } printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="${OPATH}" printf "%s\n" "${PATH}" | tr ':' '\n' | nl #--------------------------------------------------------------- function remove_from_path() { declare defaultpath newpath if [[ $# -ne 2 ]]; then defaultpath=' /usr/bin /bin /usr/sbin /sbin /usr/local/bin /usr/local/sbin /usr/local/lib /usr/local/include /usr/local/man /opt/local/bin /opt/local/sbin /opt/local/lib /opt/local/include /opt/local/man /usr/X11R6 /usr/X11R6/bin /usr/X11R6/include /usr/X11R6/lib /usr/X11R6/man ' defaultpath="${defaultpath// /}" # remove spaces defaultpath="${defaultpath//$'\n'/:}" # convert newlines into colons defaultpath="${defaultpath#:}" # cut off leading colon defaultpath="${defaultpath%:}" # cut off trailing colon printf "%s" "${defaultpath}" return 1 fi if [[ ! -d "${2}" ]]; then printf "%s" "${1}"; return 1; fi newpath="$(printf "%s" "${1//:/$'\n'}" | /usr/bin/egrep -v "^${2}$" | /usr/bin/tr '\n' ':')" #newpath="${newpath// /}" # remove spaces newpath="${newpath#:}" # cut off leading colon newpath="${newpath%:}" # cut off trailing colon #printf "%s" "${newpath//:/$'\n'}" | /usr/bin/awk '!x[$0]++' | /usr/bin/tr '\n' ':' printf "%s" "${newpath}" return 0 } printf "%s\n" "${PATH}" | tr ':' '\n' | nl export PATH="$(remove_from_path "${PATH}" "/opt/local/bin" )" export PATH="$(remove_from_path "${PATH}" "/opt/local/xyz" )" export PATH="$(remove_from_path "${PATH}" "" )" export PATH="$(remove_from_path "${PATH}" )" printf "%s\n" "${PATH}" | tr ':' '\n' | nl #--------------------------------------------------------------- man bash 2>/dev/null | less -p "Functions are executed" # Functions are executed in the context of the current shell; no new process is created to # interpret them (contrast this with the execution of a shell script).