]> code.delx.au - dotfiles/blob - .bash/functions
bash: move interactive functions to interactive file
[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: ssource /path/to/shellscript
45 # Checks if the file exists before sourcing it
46 function ssource {
47 [ -r "${1}" ] && source "${1}"
48 }
49
50 # Usage: is_function something
51 # Check if a function exists
52 function is_function {
53 declare -Ff "$1" > /dev/null
54 }
55
56 # Usage: find_up_recurse somefile
57 # Check if a file named somefile exists in this directory or any parent
58 function find_up_recurse {
59 d="$1"
60 pushd . > /dev/null
61 while [ "$PWD" != "/" ]; do
62 if [ -e "$d" ]; then
63 popd > /dev/null
64 return 0
65 fi
66 cd ..
67 done
68 popd > /dev/null
69 return 1
70 }
71
72 # Load local functions
73 ssource "${HOME}/.bash/functions_local"
74