在bash shell脚本中传播所有参数
我正在编写一个调用另一个脚本的非常简单的脚本,并且需要将参数从当前脚本传播到正在执行的脚本。
例如,我的脚本名称是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 "$@"
使用"$@"
(适用于所有POSIX兼容机)。
[...],bash具有“$ @”变量,该变量扩展为用空格分隔的所有命令行参数。
从Bash举例。
链接地址: http://www.djcxy.com/p/48571.html