]> code.delx.au - dotfiles/blob - .bash/functions
fontconfig: -apple-system font
[dotfiles] / .bash / functions
1 #! bash
2
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.
6
7
8 # Usage: pathremove /path/to/bin [PATH]
9 # Eg, to remove ~/bin from $PATH
10 # pathremove ~/bin PATH
11 function pathremove {
12 local IFS=':'
13 local NEWPATH
14 local DIR
15 local PATHVARIABLE=${2:-PATH}
16 for DIR in ${!PATHVARIABLE} ; do
17 if [ "${DIR}" != "${1}" ] ; then
18 NEWPATH="${NEWPATH:+${NEWPATH}:}${DIR}"
19 fi
20 done
21 export ${PATHVARIABLE}="${NEWPATH}"
22 }
23
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}}"
32 }
33
34 # Usage: pathappend /path/to/bin [PATH]
35 # Eg, to append ~/bin to $PATH
36 # pathappend ~/bin PATH
37 function pathappend {
38 pathremove "${1}" "${2}"
39 [ -d "${1}" ] || return
40 local PATHVARIABLE=${2:-PATH}
41 export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}${1}"
42 }
43
44 # Usage: is_function something
45 # Check if a function exists
46 function is_function {
47 declare -Ff "$1" > /dev/null
48 }
49
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 {
53 d="$1"
54 pushd . > /dev/null
55 while [ "$PWD" != "/" ]; do
56 if [ -e "$d" ]; then
57 popd > /dev/null
58 return 0
59 fi
60 cd ..
61 done
62 popd > /dev/null
63 return 1
64 }
65
66 # Load local functions
67 [ -r "${HOME}/.bash/functions_local" ] && source "${HOME}/.bash/functions_local"
68