我正在编写一个非常简单的脚本,该脚本调用另一个脚本,并且需要将参数从当前脚本传播到正在执行的脚本。
例如,我的脚本名称是foo.sh
并调用bar.sh
foo.sh:
bar $1 $2 $3 $4
如何在不显式指定每个参数的情况下执行此操作?
如果您实际上希望以相同的方式传递参数,请使用"$@"
而不是普通的$@
。
观察:
$ 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 "$@"