我将变量$ ARGS中的所有参数串联起来,并将它们全部传递给命令onece togather,就像./test_arg.sh所做的一样。如何将字符串参数传递给bash脚本中的命令?
但我发现引用字符串的参数被分割成多个。
这里是我的测试脚本,test_arg.sh执行测试用例和arg.sh只是打印上分离式线上的每个参数。
$ cat ./test_arg.sh
#!/bin/sh
ARGS="$ARGS OPT_1"
ARGS="$ARGS OPT_2"
ARGS="$ARGS 'OPT_3 is a string with space'"
echo "./arg.sh $ARGS"
echo '----------------------------'
./arg.sh $ARGS
[ [email protected] ~/tmp ]
$ cat ./arg.sh
#!/bin/sh
for var in "[email protected]"
do
echo "arg -> $var"
done
这里COMS结果:
$ ./test_arg.sh
./arg.sh OPT_1 OPT_2 'OPT_3 is a string with space'
----------------------------
arg -> OPT_1
arg -> OPT_2
arg -> 'OPT_3
arg -> is
arg -> a
arg -> string
arg -> with
arg -> space'
但是,如果我的命令后,把参数直接是否能够正常工作:
$ cat test_arg.sh
#!/bin/sh
./arg.sh OPT_1 OPT_2 'OPT_3 is a string with space'
[ [email protected] ~/tmp ]
$ sh test_arg.sh
arg -> OPT_1
arg -> OPT_2
arg -> OPT_3 is a string with space
我想这个问题是这样的bash进程报价。任何人都知道吗?
如果你真的使用'sh',你为什么标记这个'bash'? – devnull
http://mywiki.wooledge.org/BashFAQ/050是直接点。 –