0

The contents of the PATH variable on my system is as follows:

> echo $PATH
/c/Users/USER/bin:/mingw32/bin:/usr/local/bin:/usr/bin:/bin:/mingw32/bin:/usr/bin:/c/Users/USER/bin:/c/ProgramData/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Program Files/Broadcom/Broadcom 802.11 Network Adapter/Driver:/c/Program Files/Toshiba/Bluetooth Toshiba Stack/sys:/c/Program Files/Internet Explorer:/c/Program Files/Common Files/lenovo/easyplussdk/bin:/c/Program Files/Skype/Phone:/cmd:/usr/bin/vendor_perl:/usr/bin/core_perl

Now, ${variable## pattern} deletes the longest matching pattern from the beginning and returns the rest.

Hence, if arg=${PATH##:*} should match the longest pattern starting with : followed by anything. echo $arg should return /c/Users/USER/bin

However, echo $arg on my system returns the output as:

/c/Users/USER/bin:/mingw32/bin:/usr/local/bin:/usr/bin:/bin:/mingw32/bin:/usr/bin:/c/Users/USER/bin:/c/ProgramData/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Program Files/Broadcom/Broadcom 802.11 Network Adapter/Driver:/c/Program Files/Toshiba/Bluetooth Toshiba Stack/sys:/c/Program Files/Internet Explorer:/c/Program Files/Common Files/lenovo/easyplussdk/bin:/c/Program Files/Skype/Phone:/cmd:/usr/bin/vendor_perl:/usr/bin/core_perl

Similarily, echo ${PATH#:*} returns the same output

/c/Users/USER/bin:/mingw32/bin:/usr/local/bin:/usr/bin:/bin:/mingw32/bin:/usr/bin:/c/Users/USER/bin:/c/ProgramData/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Program Files/Broadcom/Broadcom 802.11 Network Adapter/Driver:/c/Program Files/Toshiba/Bluetooth Toshiba Stack/sys:/c/Program Files/Internet Explorer:/c/Program Files/Common Files/lenovo/easyplussdk/bin:/c/Program Files/Skype/Phone:/cmd:/usr/bin/vendor_perl:/usr/bin/core_perl 

Please help me understand why it's happening this way. Thank you

5
  • You need ${var%%:*} to remove the suffix and ${var##*:} to remove the prefix (greedily, that is) Oct 24, 2016 at 10:20
  • yeah, ${var%%:*} seems to be working fine but why wouldn't #{var##:*} work the same way?
    – MathMan
    Oct 24, 2016 at 10:22
  • 1
    Because pattern in ${variable##pattern} is supposed to match the beginning of the value of variable... so, in your case, the beginning of PATH does not match :*. Read the manual. Oct 24, 2016 at 10:26
  • I got it. I got confused earlier. Thank you
    – MathMan
    Oct 24, 2016 at 10:27
  • With bash's pattern substitution, ${PATH/:*} should also expand to /c/Users/USER/bin. (It finds the first match :*, but isn't locked to the start of the string.)
    – ilkkachu
    Oct 24, 2016 at 10:31

1 Answer 1

2

You have put the * glob token in wrong place, put if before : as you are using ## (or #) to get rid of the portion from left:

${PATH#*:}  ## Non-greedy
${PATH##*:}  ## Greedy

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .