gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:46:03 24 4
gpt4 key购买 nike

如何将一个 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.

关于shell - 在 shell 脚本之间传递参数但保留引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1987162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com