]> code.delx.au - dotfiles/blob - .bash/functions
Use nicer function syntax
[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 # Sets colours to be appropriate for a light on dark terminal
51 function darkterm {
52 DARK=1
53 source "${HOME}/.bash/colors"
54 }
55
56 # Sets the colours to be appropriate for a dark on light terminal
57 function lightterm {
58 DARK=0
59 source "${HOME}/.bash/colors"
60 }
61
62 # Usage: mcd somedir
63 # Creates the directory if it doesn't exist, and changes into it
64 function mcd {
65 mkdir -p "${1}" &&
66 cd "${1}"
67 }
68
69
70 # Load local functions
71 ssource "${HOME}/.bash/functions_local"
72