我以为发布了一个问答,因为我没有找到任何类似的东西。如果已经存在,请将其标记为重复。
在Bash shell下运行的以下代码不起作用(应该只返回f,$ @中的最后(-1)项):
- $ set -- a b c d e f
- $ echo ${@:-1}
- a b c d e f
$ {parameter:-word}是一种类型为
parameter expansion:
${parameter:-word}
If parameter is unset or null,the expansion of word is substituted.
Otherwise,the value of parameter is substituted.
所以$ {@: – 1}被解释为:
>如果$ @未设置或为null,则替换为1
否则就要离开$ @
由于后者是真实的,所以回声打印$ @所有的荣耀
修复:
在 – 之间放置一些空格: –
- $ echo ${@: -1}
- f