处理除第一个参数以外的所有参数(在 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 找到了这个维基,但它提供了一个文字示例:
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屋!