3 # Useful bash functions. This is sourced by the environment file.
4 # These are available to scripts, but you shouldn't use them in scripts if you
5 # want them to be portable.
8 # Usage: pathremove /path/to/bin [PATH]
9 # Eg, to remove ~/bin from $PATH
10 # pathremove ~/bin PATH
15 local PATHVARIABLE=${2:-PATH}
16 for DIR in ${!PATHVARIABLE} ; do
17 if [ "${DIR}" != "${1}" ] ; then
18 NEWPATH="${NEWPATH:+${NEWPATH}:}${DIR}"
21 export ${PATHVARIABLE}="${NEWPATH}"
24 # Usage: pathprepend /path/to/bin [PATH]
25 # Eg, to prepend ~/bin to $PATH
26 # pathprepend ~/bin PATH
27 function pathprepend {
28 pathremove "${1}" "${2}"
29 [ -d "${1}" ] || return
30 local PATHVARIABLE="${2:-PATH}"
31 export ${PATHVARIABLE}="${1}${!PATHVARIABLE:+:${!PATHVARIABLE}}"
34 # Usage: pathappend /path/to/bin [PATH]
35 # Eg, to append ~/bin to $PATH
36 # pathappend ~/bin PATH
38 pathremove "${1}" "${2}"
39 [ -d "${1}" ] || return
40 local PATHVARIABLE=${2:-PATH}
41 export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}${1}"
44 # Usage: is_function something
45 # Check if a function exists
46 function is_function {
47 declare -Ff "$1" > /dev/null
50 # Usage: find_up_recurse somefile
51 # Check if a file named somefile exists in this directory or any parent
52 function find_up_recurse {
55 while [ "$PWD" != "/" ]; do
66 # Load local functions
67 [ -r "${HOME}/.bash/functions_local" ] && source "${HOME}/.bash/functions_local"