Rearrange default UNIX PATHs priority. Under Cygwin that helps with placing Cygwin's paths before Windows'.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Wed, 16 Jun 2021 13:39:23 +0300
changeset 988 b5c31b623dec
parent 987 e0ffdc132f41
child 989 5b3759d3d789
Rearrange default UNIX PATHs priority. Under Cygwin that helps with placing Cygwin's paths before Windows'.
.env.bash
--- a/.env.bash	Thu Jun 03 02:02:50 2021 +0300
+++ b/.env.bash	Wed Jun 16 13:39:23 2021 +0300
@@ -22,31 +22,38 @@
 # Easy cd to often used dirs.
 export CDPATH=.:$HOME:$HOME/devel:$HOME/my
 
-if [[ :$PATH: != *:/usr/sbin:* ]]; then
-  export PATH=/usr/sbin:$PATH
-fi
-if [[ :$PATH: != *:/sbin:* ]]; then
-  export PATH=/sbin:$PATH
-fi
-if [[ :$PATH: != *:/usr/local/bin:* ]]; then
-  export PATH=$PATH:/usr/local/bin
-fi
+function my_path_prune() {
+  if [[ :$1: =~ (.*):$2:(.*) ]]; then
+    local left=${BASH_REMATCH[1]}
+    local right=${BASH_REMATCH[2]}
+    [[ ${left:0:1} = : ]] && left=${left:1}
+    [[ ${right: -1} = : ]] && right=${right:0: -1}
+    if [[ -z $left ]]; then
+      echo "$right"
+    elif [[ -z $right ]]; then
+      echo "$left"
+    else
+      echo "$left:$right"
+    fi
+  else
+    echo "$1"
+  fi
+}
+
+function my_path_preappend() {
+  PATH=$1:$(my_prune_path "$PATH" $1)
+}
+
+# For my "cygrun.exe" wrapper to work it should be behind shebang scripts in the PATH.
+my_path_preappend ~/usr/bin
 # ~/.local/bin is default PATH for "pip install --user $PKG".
-if [[ :$PATH: != *:$HOME/.local/bin:* ]]; then
-  export PATH=$PATH:$HOME/.local/bin
-fi
-if [[ :$PATH: =~ (.*):$HOME/usr/bin:(.*) ]]; then
-  # For my "cygrun.exe" wrapper to work it should be behind shebang scripts in the PATH.
-  if [[ $OSTYPE = cygwin ]]; then
-    [[ ${BASH_REMATCH[1]:0:1} = : ]] && _left=${BASH_REMATCH[1]:1}
-    [[ ${BASH_REMATCH[2]: -1} = : ]] && _right=${BASH_REMATCH[2]:0: -1}
-    PATH=$_left:$_right:$HOME/usr/bin
-    unset _left _right
-  fi
-else
-  export PATH=$PATH:$HOME/usr/bin
-fi
-
+my_path_preappend ~/.local/bin
+my_path_preappend /usr/local/bin
+my_path_preappend /usr/bin
+my_path_preappend /bin
+my_path_preappend /usr/sbin
+my_path_preappend /sbin
+export PATH
 
 if [[ -z $LD_LIBRARY_PATH ]]; then
   LD_LIBRARY_PATH=$HOME/usr/lib