在 shell 脚本之间传递参数但保留引号 [英] pass arguments between shell scripts but retain quotes
问题描述
如何将一个 shell 脚本的所有参数传递给另一个?我已经尝试过 $*,但正如我所料,如果您引用了参数,这将不起作用.
How do I pass all the arguments of one shell script into another? I have tried $*, but as I expected, that does not work if you have quoted arguments.
示例:
$ 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
推荐答案
使用 "$@"
而不是 $*
来保留引号:
Use "$@"
instead of $*
to preserve the quotes:
./script2.sh "$@"
更多信息:
http://tldp.org/LDP/abs/html/internalvariables.html
$*
所有的位置参数,看成一个词
$*
All of the positional parameters, seen as a single word
注意:$*"必须加引号.
Note: "$*" must be quoted.
$@
与 $* 相同,但每个参数都是一个带引号的字符串,即参数原封不动地传递,没有解释或扩展.这意味着,除其他外,参数中的每个参数list 被视为一个单独的词.
$@
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.
这篇关于在 shell 脚本之间传递参数但保留引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!