处理除第一个参数以外的所有参数(在bash脚本中) [英] Process all arguments except the first one (in a bash script)
问题描述
我有一个简单的脚本,其中第一个参数保留给文件名,所有其他可选参数应传递给脚本的其他部分.
I have a simple script where the first argument is reserved for the filename, and all other optional arguments should be passed to other parts of the script.
我使用Google找到了此Wiki ,但它提供了一个字面示例:
Using Google I found this wiki, but it provided a literal example:
echo "${@: -1}"
我什么都无法工作,例如:
I can't get anything else to work, like:
echo "${@:2}"
或
echo "${@:2,1}"
我从终端获得错误替换".
I get "Bad substitution" from the terminal.
问题是什么,除了传递给bash脚本的第一个参数外,我该如何处理?
What is the problem, and how can I process all but the first argument passed to a bash script?
推荐答案
使用此:
echo "${@:2}"
以下语法:
The following syntax:
echo "${*:2}"
也可以使用,但不建议使用,因为它是 @ Gordon 已经解释过,使用*
,它会将所有参数一起作为带有空格的单个参数运行,而@
保留它们之间的间隔(即使某些参数本身包含空格).它与echo
没什么区别,但是对许多其他命令来说很重要.
would work as well, but is not recommended, because as @Gordon already explained, that using *
, it runs all of the arguments together as a single argument with spaces, while @
preserves the breaks between them (even if some of the arguments themselves contain spaces). It doesn't make the difference with echo
, but it matters for many other commands.
这篇关于处理除第一个参数以外的所有参数(在bash脚本中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!