通过bash脚本将参数传递到/ bin / bash

JCO*_*idl 4 bash

我正在编写一个bash脚本,该脚本需要多个命令行参数(可能包括空格),并将所有参数通过登录shell传递给程序(/ bin / some_program)。从bash脚本调用的登录shell将取决于用户的登录shell。假设在此示例中,用户使用/ bin / bash作为其登录shell ...但是它可能是/ bin / tcsh或其他任何东西。

如果我知道将有多少个参数传递给some_program,则可以在bash脚本中添加以下行:

#!/bin/bash
# ... (some lines where we determine that the user's login shell is bash) ...
/bin/bash --login -c "/bin/some_program \"$1\" \"$2\""
Run Code Online (Sandbox Code Playgroud)

然后按以下方式调用上述脚本:

my_script "this is too" cool
Run Code Online (Sandbox Code Playgroud)

通过上面的示例,我可以确认some_program接收到两个参数“ this too too”和“ cool”。

我的问题是...如果我不知道会传递多少个参数怎么办?我想将所有发送到my_script的参数传递给some_program。问题是我不知道该怎么做。以下是一些无效的内容

/bin/bash --login -c "/bin/some_program $@"     # --> 3 arguments: "this","is","too"
/bin/bash --login -c /bin/some_program "$@"     # --> passes no arguments
Run Code Online (Sandbox Code Playgroud)

the*_*mel 5

在bash手册中引用以下内容-c

如果存在-c选项,则从字符串读取命令。如果字符串后面有参数,则将它们分配给位置参数,从$ 0开始

为我工作:

$ cat x.sh
#!/bin/bash
/bin/bash --login -c 'echo 1:$1 2:$2 3:$3' echo "$@"
$ ./x.sh "foo bar" "baz" "argh blargh quargh"
1:foo bar 2:baz 3:argh blargh quargh
Run Code Online (Sandbox Code Playgroud)

我不知道您是如何得出“无争议通过”结论的,也许您错过了$0一点?