# -*- mode: sh -*- # Remove all occurrences of $1 from the path on stdin, and add # it to the front. 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. move_to_back () { sed \ -e "s|:|::|g" \ -e "s|..*|:&:|" \ -e "s|:$1:||g" \ -e "s|\$|:$1:|" \ -e "s|^:||" \ -e "s|:$||" \ -e "s|::*|:|g" } prepend () { eval "export $1=\"\$(echo \"\$$1\" | move_to_front '$2')\"" } postpend () { eval "export $1=\"\$(echo \"\$$1\" | move_to_back '$2')\"" } prepend_if_dir () { if [ -d "$2" ]; then prepend $1 "$2" fi } postpend_if_dir () { if [ -d "$2" ]; then postpend $1 "$2" fi } add_path () { prepend_if_dir PATH "$1" } add_path_after () { postpend_if_dir PATH "$1" } add_bin_man () { prepend_if_dir PATH $1/bin prepend_if_dir INFOPATH $1/info # 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. } add_bin_man_after () { postpend_if_dir PATH "$1/bin" postpend_if_dir INFOPATH "$1/info" } add_library_path () { prepend_if_dir LD_LIBRARY_PATH "$1" }