Propagate all arguments in a bash shell script

I am writing a very simple script that calls another script, and I need to propagate the parameters from my current script to the script I am executing.

For instance, my script name is foo.sh and calls bar.sh

foo.sh:

bar $1 $2 $3 $4

How can I do this without explicitly specifying each parameter?


Use "$@" instead of plain $@ if you actually wish your parameters to be passed the same.

Observe:

$ cat foo.sh
#!/bin/bash
baz.sh $@

$ cat bar.sh
#!/bin/bash
baz.sh "$@"

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

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

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

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

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

对于bash和其他类似Bourne的炮弹:

java com.myserver.Program "$@"

Use "$@" (works for all POSIX compatibles).

[...] , bash features the "$@" variable, which expands to all command-line parameters separated by spaces.

From Bash by example.

链接地址: http://www.djcxy.com/p/48572.html

上一篇: 如何在bash脚本中使用curl来发布JSON变量?

下一篇: 在bash shell脚本中传播所有参数