【发布时间】:2010-12-23 15:52:12
【问题描述】:
$1 是第一个参数。$@ 是所有参数。
如何找到传递给 shell 的最后一个参数 脚本?
【问题讨论】:
-
我使用的是 bash,但解决方案越便携越好。
-
使用也可以使用${ !# }
-
仅适用于bash,Kevin Little's answer 建议使用简单的
${!#}。使用bash -c 'echo ${!#}' arg1 arg2 arg3对其进行测试。对于bash、ksh 和zsh,Dennis Williamson's answer 建议${@: -1}。此外${*: -1}也可以使用。使用zsh -c 'echo ${*: -1}' arg1 arg2 arg3对其进行测试。但这不适用于dash、csh 和tcsh。 -
${!#}与${@: -1}不同,它也适用于参数扩展。您可以使用bash -c 'echo ${!#%.*}' arg1.out arg2.out arg3.out进行测试。