[英]Shell script parameter substitution
Can someone tell me what the difference is between these two conventions for getting the name of the currently running script? 有人能告诉我这两个约定之间有什么区别来获取当前正在运行的脚本的名称?
#!/bin/sh
echo $0
echo ${0##*/}
Does the second version strip the path from the script name? 第二个版本是否从脚本名称中删除路径?
Thanks! 谢谢!
Your guess is correct. 你的猜测是正确的。 From the
bash
docs on my machine: 从我的机器上的
bash
文档:
${parameter#word} ${parameter##word} The word is expanded to produce a pattern just as in pathname expansion. If the pattern matches the beginning of the value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ``#'' case) or the longest matching pattern (the ``##'' case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
Yes, second version uses bash extension to cut longest prefix of $0 which match to */ regexp, so if $0 is "/bin/bash", then ${0##*/} will be "bash". 是的,第二个版本使用bash扩展来剪切与* / regexp匹配的$ 0的最长前缀,所以如果$ 0是“/ bin / bash”,那么$ {0 ## * /}将是“bash”。 You can use `basename $0` to achieve the same result.
您可以使用`basename $ 0`来获得相同的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.