initial commit
[distro-setup] / path-add-function
1 #!/bin/bash
2 # no bashisms so it can be used in debian profile run by dash
3 # --start adds to start of path, which will give it highest priority
4 # --ifexists will add to path only if the directory exists
5 path-add() {
6 local found x y z
7 local ifexists=false
8 local start=false
9 while [ "$1" = --* ]; do
10 if [ "$1" = --start ]; then
11 start=true
12 elif [ "$1" = --ifexists ]; then
13 ifexists=true
14 fi
15 shift
16 done
17 for x in "$@"; do
18 found=false
19 IFS=:
20 for y in $PATH; do
21 [ "$x" = "$y" ] && found=true
22 done
23 unset IFS
24 if ! $found; then
25 if [ $ifexists = false ] || [ -d $x ]; then
26 if $start; then
27 PATH="$x:$PATH"
28 else
29 PATH="$PATH:$x"
30 fi
31 fi
32 fi
33 done
34 }