pass arguments between shell scripts but retain quotes

自古美人都是妖i 提交于 2019-12-03 05:36:21

问题


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.

Example:

$ 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

I want it to print out:

apple
pear orange
banana

回答1:


Use "$@" instead of $* to preserve the quotes:

./script2.sh "$@"

More info:

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.



来源:https://stackoverflow.com/questions/1987162/pass-arguments-between-shell-scripts-but-retain-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!