about summary refs log tree commit diff stats
path: root/profile/00_functions.sh
diff options
context:
space:
mode:
Diffstat (limited to 'profile/00_functions.sh')
-rw-r--r--profile/00_functions.sh62
1 files changed, 62 insertions, 0 deletions
diff --git a/profile/00_functions.sh b/profile/00_functions.sh new file mode 100644 index 0000000..a94eaee --- /dev/null +++ b/profile/00_functions.sh
@@ -0,0 +1,62 @@
1# utility functions for all shells
2# these should be POSIX-compatible.
3
4# add a path to PATH, but only if it's not already there
5path_add_to_PATH() { # path_add [-a] PATH...
6 # -a appends (default is prepend)
7 APPEND=false
8 if [ "x$1" = "x-a" ]; then
9 APPEND=true
10 shift
11 fi
12
13 for p; do
14 case ":$PATH:" in
15 *:"$p":*) ;;
16 *)
17 if $APPEND; then
18 PATH="$PATH:$p"
19 else
20 PATH="$p:$PATH"
21 fi
22 ;;
23 esac
24 done
25
26 unset APPEND
27}
28
29# Generalization of `path_add_to_PATH' for any variable.
30path_add_unsafe() { #path_add_unsafe [-a] [-d DELIM] VAR PATH...
31 ## Add PATH... to VAR, delimiting with DELIM (default :).
32 # By default, the VAR will be prepended to; passing -a will append the
33 # variable. -d DELIM defines the delimiter.
34 #
35 # This function has the _unsafe suffix because it uses `eval' to set
36 # variables.
37 APPEND=false; DELIM=:
38 while getopts ad: opt; do
39 case "$opt" in
40 a) APPEND=true ;;
41 d) DELIM="$OPTARG" ;;
42 *) return 1 ;;
43 esac
44 done
45 shift $(expr $OPTIND - 1)
46
47 var="$1"; shift
48
49 for path; do
50 case ":$(eval "echo \$$var"):" in
51 *:"$path":*) ;;
52 *)
53 if $APPEND; then
54 eval "$var=\$$var${var:+$DELIM}$path"
55 else
56 eval "$var=$path${var:+$DELIM}\$$var"
57 fi
58 ;;
59 esac
60 done
61 unset -v APPEND DELIM var
62}