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

标签 shell argument-passing

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

相关文章:

bash - 为什么我的脚本在命令后突然退出?

macros - 在宏中,我可以让 ,var 评估为空白/无吗?

java - 将参数传递给实现的 java 方法

c - 在可变参数宏中引用特定参数

javascript - 如何在 JavaScript 中检查参数是否是对象(而不是数组)

linux - 如何在Linux中循环中按值打印数组?

mysql - 关于MySQL错误日志中任何错误的电子邮件通知

laravel-4 - 如何使用 Controller 处理 Laravel 4 子域路由,将子域作为参数传递

linux - 如何获取相对于指定日期的下一小时?

Linux Shell 脚本查找文件并重命名