make path_add more correct for dash
[distro-setup] / path_add-function
index 6f06ed9b2c95dba2766fda574750ca04b3737759..428e2ecabc2faef3359efcb6cd61783ae32ae50b 100644 (file)
@@ -3,17 +3,25 @@
 # --start adds to start of path, which will give it highest priority
 # --ifexists will add to path only if the directory exists
 path_add() {
-    local found x y z
-    local ifexists start
+    local found x y z ifexists start loop
     ifexists=false
     start=false
-    while [ "$1" = --* ]; do
-        if [ "$1" = --start ]; then
-            start=true
-        elif [ "$1" = --ifexists ]; then
-            ifexists=true
-        fi
-        shift
+    loop=true
+    # portable substring matching is ugly http://mywiki.wooledge.org/BashFAQ/041
+    while $loop; do
+        case $1 in
+            --*)
+                if [ "$1" = --start ]; then
+                    start=true
+                elif [ "$1" = --ifexists ]; then
+                    ifexists=true
+                fi
+                shift
+                ;;
+            *)
+                loop=false
+                ;;
+        esac
     done
     for x in "$@"; do
         found=false
@@ -23,7 +31,7 @@ path_add() {
         done
         unset IFS
         if ! $found; then
-            if ! $ifexists || [ -d $x ]; then
+            if ! $ifexists || [ -d "$x" ]; then
                 if [ ! "$PATH" ]; then
                     PATH="$x"
                 elif $start; then