#!/bin/bash # Remove all occurrences of $1 from the path on stdin, and add # it to the front. function move_to_front { # First, give each element its own leading and trailing colon: # foo:bar -> :foo::bar:. Then remove all instances of $1, add one # to the front, and put back in normal path form. sed \ -e "s|:|::|g" \ -e "s|..*|:&:|" \ -e "s|:$1:||g" \ -e "s|^|:$1:|" \ -e "s|^:||" \ -e "s|:$||" \ -e "s|::*|:|g" } # Remove all occurrences of $1 from the path on stdin, and add # it to the end. function move_to_back { sed \ -e "s|:|::|g" \ -e "s|..*|:&:|" \ -e "s|:$1:||g" \ -e "s|\$|:$1:|" \ -e "s|^:||" \ -e "s|:$||" \ -e "s|::*|:|g" } function prepend { eval "export $1=\"\$(echo \"\$$1\" | move_to_front '$2')\"" } function postpend { eval "export $1=\"\$(echo \"\$$1\" | move_to_back '$2')\"" } function prepend-if-dir { if [ -d $2 ]; then prepend $1 $2 fi } function postpend-if-dir { if [ -d $2 ]; then postpend $1 $2 fi } function add-path { prepend-if-dir PATH $1 } function add-path-after { postpend-if-dir PATH $1 } function add-bin-man { prepend-if-dir PATH $1/bin prepend-if-dir INFOPATH $1/info prepend-if-dir LD_LIBRARY_PATH $1/lib # You might think we should set MANPATH here too, but Linux man # automatically checks for a 'man' subdirectory associated with # each directory in PATH, so we don't need to do the equivalent # here. And setting MANPATH at all overrides that automatically # generated search list; since MANPATH is initially unset, but # PATH isn't, setting MANPATH can actually *reduce* the set of # directories man will search. } function add-bin-man-after { postpend-if-dir PATH $1/bin postpend-if-dir INFOPATH $1/info postpend-if-dir LD_LIBRARY_PATH $1/lib } function add-library-path { case ":$LD_LIBRARY_PATH:" in *":$1:"* ) ;; * ) if [ -d $1 ]; then postpend LD_LIBRARY_PATH $1 fi ;; esac }