加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 20:05:18 所属栏目:安全 来源:网络整理
导读:如何将一个shell脚本的所有参数传递给另一个?我试过$ *,但正如我所料,如果你引用的参数,这不工作。 例: $ cat script1.sh#! /bin/sh./script2.sh $*$ cat script2.sh#! /bin/shecho $1echo $2echo $3$ script1.sh apple "pear orange" bananaapplepearo
如何将一个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读