Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell脚本参数替换_Shell_Variables_Parameters - Fatal编程技术网

Shell脚本参数替换

Shell脚本参数替换,shell,variables,parameters,Shell,Variables,Parameters,有人能告诉我这两种获取当前正在运行的脚本名称的约定之间有什么区别吗 #!/bin/sh echo $0 echo ${0##*/} 第二个版本是否从脚本名中删除路径 谢谢 是的,第二个版本使用bash扩展来剪切与*/regexp匹配的最长前缀$0,因此如果$0是“/bin/bash”,那么${0##*/}将是“bash”。您可以使用'basename$0'获得相同的结果。您的猜测是正确的。从我的机器上的bash文档: ${parameter#word} ${parameter##wor

有人能告诉我这两种获取当前正在运行的脚本名称的约定之间有什么区别吗

#!/bin/sh

echo $0
echo ${0##*/}
第二个版本是否从脚本名中删除路径


谢谢

是的,第二个版本使用bash扩展来剪切与*/regexp匹配的最长前缀$0,因此如果$0是“/bin/bash”,那么${0##*/}将是“bash”。您可以使用'basename$0'获得相同的结果。

您的猜测是正确的。从我的机器上的
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. ${parameter#word} ${parameter##word} 这个词被展开以产生一个模式,就像在路径名中一样 膨胀如果模式与值的开头匹配 参数,则展开的结果是展开的 具有最短匹配模式的参数值(`#'' 大小写)或删除最长的匹配模式(“大小写”)。 如果参数为@或*,则应用图案移除操作 依次添加到每个位置参数,展开式为 结果列表。如果参数是下标的数组变量 对于@或*,模式移除操作应用于每个 依次是数组的成员,扩展是结果 列表