在shell脚本之间传递参数,但保留引号

如何将一个shell脚本的所有参数传递给另一个?我试过$ *,但正如我所料,如果你引用的参数,这不工作。

例:

$ cat script1.sh

#! /bin/sh
./script2.sh $*

$ cat script2.sh

#! /bin/sh
echo $1
echo $2
echo $3

$ script1.sh apple "pear orange" banana
apple
pear
orange

我想打印出来:

apple
pear orange
banana
使用“$ @”而不是$ *来保留引号:

./script2.sh "$@"

更多信息:

http://tldp.org/LDP/abs/html/internalvariables.html

$*
All of the positional parameters, seen as a single word

Note: “$*” must be quoted.

$@
Same as $*, but each parameter is a quoted string, that is, the
parameters are passed on intact, without interpretation or expansion.
This means, among other things, that each parameter in the argument
list is seen as a separate word.

Note: Of course, “$@” should be quoted.

http://stackoverflow.com/questions/1987162/pass-arguments-between-shell-scripts-but-retain-quotes

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:在shell脚本之间传递参数,但保留引号