Cleaner appending to PATH
dblume

dblume commited on 2022-12-15 15:10:44
Showing 2 changed files, with 12 additions and 4 deletions.

... ...
@@ -15,5 +15,7 @@ if [[ $(uname -s) == Darwin* ]]; then
15 15
         alias ctags="$(brew --prefix)/bin/ctags"
16 16
     fi
17 17
     # Set default names for GNU grep, sed and find
18
-    PATH="/usr/local/opt/grep/libexec/gnubin:/usr/local/opt/gnu-sed/libexec/gnubin:/usr/local/opt/findutils/libexec/gnubin:$PATH"
18
+    prepend_to_path /usr/local/opt/grep/libexec/gnubin
19
+    prepend_to_path /usr/local/opt/gnu-sed/libexec/gnubin
20
+    prepend_to_path /usr/local/opt/findutils/libexec/gnubin
19 21
 fi
... ...
@@ -58,12 +58,18 @@ export PS1='$(if [ $? -eq 0 ]; then echo -e "\[\e[32m\]\xe2\x9c\x93";
58 58
 
59 59
 set -o vi
60 60
 
61
-add_to_path() {
61
+append_to_path() {
62 62
     if [ -d "$1" ] && [[ ! $PATH =~ (^|:)$1(:|$) ]]; then
63 63
         PATH+=:$1
64 64
     fi
65 65
 }
66 66
 
67
+prepend_to_path() {
68
+    if [ -d "$1" ] && [[ ! $PATH =~ (^|:)$1(:|$) ]]; then
69
+        PATH=$1:$PATH
70
+    fi
71
+}
72
+
67 73
 # if this is a CygWin .bashrc, then set CygWin's commands first in PATH
68 74
 # because link.exe and find.exe exist in Windows's path.
69 75
 # Add /usr/lib/lapack at the end so python's numpy can find lapack_lite
... ...
@@ -71,7 +77,7 @@ add_to_path() {
71 77
 if [[ $(uname -s) == CYGWIN* ]]; then
72 78
     PATH=/usr/local/bin:/usr/bin:$PATH
73 79
     PATH=${PATH//":/usr/local/bin:/usr/bin"/} # delete any instances in middle
74
-    add_to_path /usr/lib/lapack
80
+    append_to_path /usr/lib/lapack
75 81
     export GIT_SSH=/cygdrive/c/cygwin64/bin/ssh
76 82
     ulimit -n 1024 # for "duplicity"
77 83
 elif [[ -n "${WSL_DISTRO_NAME}" ]]; then
... ...
@@ -107,7 +113,7 @@ alias tmux='tmux -2u'
107 113
 export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
108 114
 
109 115
 # Add to PATH only if not already in PATH.
110
-add_to_path $HOME/bin
116
+append_to_path $HOME/bin
111 117
 
112 118
 # For interactive shells ('i' in $-), disable stty flow control (ctrl+s,ctrl+q)
113 119
 case "$-" in
114 120