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

前端之家收集整理的这篇文章主要介绍了在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.

猜你在找的Bash相关文章