我的系统上PATH变量的内容如下:
> 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
现在,${variable## pattern}
从开头删除最长的匹配模式并返回其余的。
因此, ifarg=${PATH##:*}
应该匹配最长的模式,以 开头,:
后跟任何内容。
echo $arg
应该返回/c/Users/USER/bin
但是,echo $arg
在我的系统上返回输出为:
/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
类似地,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
请帮助我理解为什么会这样。谢谢
答案1
您已将*
glob 标记放在错误的位置,请将 if 放在前面,:
因为您正在使用##
(或#
) 来删除左侧的部分:
${PATH#*:} ## Non-greedy
${PATH##*:} ## Greedy