如何将传递给我的 bash 脚本的所有参数传递给我的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3811345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:42:13  来源:igfitidea点击:

How to pass all arguments passed to my bash script to a function of mine?

bashfunctionparameter-passing

提问by devoured elysium

Let's say I have defined a function abc()that will handle the logic related to analyzing the arguments passed to my script.

假设我已经定义了一个function abc()将处理与分析传递给我的脚本的参数相关的逻辑。

How can I pass all arguments my bash script has received to it? The number of params is variable, so I can't just hardcode the arguments passed like this:

如何将我的 bash 脚本收到的所有参数传递给它?参数的数量是可变的,所以我不能像这样对传递的参数进行硬编码:

abc    

Edit. Better yet, is there any way for my function to have access to the script arguments' variables?

编辑。更好的是,我的函数有什么方法可以访问脚本参数的变量吗?

回答by Gordon Davisson

The $@variable expands to all command-line parameters separated by spaces. Here is an example.

$@变量扩展为所有以空格分隔的命令行参数。这是一个例子。

abc "$@"

When using $@, you should (almost) always put it in double-quotes to avoid misparsing of arguments containing spaces or wildcards. This works for multiple arguments. "$*"will be passed as one long string.

使用 时$@,您应该(几乎)始终将其放在双引号中,以避免错误解析包含空格或通配符的参数。这适用于多个参数。 "$*"将作为一个长字符串传递。

It is also worth nothing that $0is not in $@.

还值得什么,$0是不是$@

The Bash Reference Manual Special Parameters Sectionsays that $@expands to the positional parameters starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is "$@"is equivalent to "$1" "$2" "$3"....

猛砸参考手册特殊参数章节说,$@扩展为从1开始的位置参数。当扩展发生在双引号内时,每个参数都扩展为一个单独的词。那"$@"相当于"$1" "$2" "$3"....

If you want to pass all but the firstarguments, you can first use shiftto "consume" the first argument and then pass $@to pass the remaining arguments to another command. In bash (but not in plain POSIX shells), you can do this without messing with the argument list using a variant of array slicing: "${@:3}"will get you the arguments starting with "$3". "${@:3:4}"will get you up to four arguments starting at "$3"(i.e. "$3" "$4" "$5" "$6"), if that many arguments were passed.

如果您想传递除第一个参数之外的所有参数,您可以先使用shiftto “consume”第一个参数,然后$@pass 将其余参数传递给另一个命令。在 bash 中(但不是在普通的 POSIX shell 中),您可以使用数组切片的变体在不弄乱参数列表的情况下执行此操作:"${@:3}"将为您提供以"$3". 如果传递了那么多参数,"${@:3:4}"将从"$3"(ie "$3" "$4" "$5" "$6")开始最多为您提供四个参数。

回答by hajamie

I needed a variation on this, which I expect will be useful to others:

我需要对此进行修改,我希望这对其他人有用:

function diffs() {
        diff "${@:3}" <(sort "") <(sort "")
}

The "${@:3}"part means all the members of the array starting at 3. So this function implements a sorted diff by passing the first two arguments to diff through sort and then passing all other arguments to diff, so you can call it similarly to diff:

"${@:3}"部分表示从 3 开始的数组的所有成员。 因此,该函数通过将前两个参数通过 sort 传递给 diff 然后将所有其他参数传递给 diff 来实现已排序的 diff,因此您可以类似于 diff 调用它:

diffs file1 file2 [other diff args, e.g. -y]

回答by Mia Clarke

Use the $@variable, which expands to all command-line parameters separated by spaces.

使用$@变量,它扩展到所有以空格分隔的命令行参数。

abc "$@"

回答by Giuseppe Cardone

Here's a simple script:

这是一个简单的脚本:

#!/bin/bash

args=("$@")

echo Number of arguments: $#
echo 1st argument: ${args[0]}
echo 2nd argument: ${args[1]}

$#is the number of arguments received by the script. I find easier to access them using an array: the args=("$@")line puts all the arguments in the argsarray. To access them use ${args[index]}.

$#是脚本接收的参数数量。我发现使用数组更容易访问它们:该args=("$@")行将所有参数放在args数组中。要访问它们,请使用${args[index]}.

回答by robstarbuck

It's worth mentioning that you can specify argument ranges with this syntax.

值得一提的是,您可以使用此语法指定参数范围。

function example() {
    echo "line1 ${@:1:1}"; #First argument
    echo "line2 ${@:2:1}"; #Second argument
    echo "line3 ${@:3}"; #Third argument onwards
}

I hadn't seen it mentioned.

我没有看到它提到。

回答by Vivien Barousse

abc "$@"

$@represents all the parameters given to your bash script.

$@代表给你的 bash 脚本的所有参数。

回答by andrew lorien

abc "$@" is generally the correct answer. But I was trying to pass a parameter through to an su command, and no amount of quoting could stop the error su: unrecognized option '--myoption'. What actually worked for me was passing all the arguments as a single string :

abc "$@" 通常是正确的答案。但是我试图将参数传递给 su 命令,并且再多的引用也无法阻止错误su: unrecognized option '--myoption'。实际上对我有用的是将所有参数作为单个字符串传递:

abc "$*"

My exact case (I'm sure someone else needs this) was in my .bashrc

我的确切情况(我确定其他人需要这个)在我的 .bashrc 中

# run all aws commands as Jenkins user
aws ()
{
    sudo su jenkins -c "aws $*"
}