小编典典

将所有参数从 Bash 脚本传递给另一个命令

all

我正在编写一个非常简单的脚本来调用另一个脚本,我需要将参数从我当前的脚本传播到我正在执行的脚本。

例如,我的脚本名称是foo.sh并且调用bar.sh

foo.sh:

bar $1 $2 $3 $4

在不明确指定每个参数的情况下如何做到这一点?


阅读 127

收藏
2022-05-07

共1个答案

小编典典

如果您确实希望传递相同的参数,请使用"$@"而不是plain 。$@

观察:

$ cat no_quotes.sh
#!/bin/bash
echo_args.sh $@

$ cat quotes.sh
#!/bin/bash
echo_args.sh "$@"

$ cat echo_args.sh
#!/bin/bash
echo Received: $1
echo Received: $2
echo Received: $3
echo Received: $4

$ ./no_quotes.sh first second
Received: first
Received: second
Received:
Received:

$ ./no_quotes.sh "one quoted arg"
Received: one
Received: quoted
Received: arg
Received:

$ ./quotes.sh first second
Received: first
Received: second
Received:
Received:

$ ./quotes.sh "one quoted arg"
Received: one quoted arg
Received:
Received:
Received:
2022-05-07