我想运行一些命令,让我们从我的bash脚本命名为"test",然后从bash变量中添加一些params.
我的剧本:
#!/bin/bash -x
PARAMS="-A 'Foo' -B 'Bar'"
./test $PARAMS
Run Code Online (Sandbox Code Playgroud)
我有:
+ PARAMS='-A '\''Foo'\'' -B '\''Bar'\'''
+ ./test -A ''\''Foo'\''' -B ''\''Bar'\'''
Run Code Online (Sandbox Code Playgroud)
这是不对的!
另一个案例:
#!/bin/bash -x
PARAMS='-A '"'"'Foo'"'"' -B '"'"'Bar'"'"
./test $PARAMS
Run Code Online (Sandbox Code Playgroud)
结果也很悲伤:
+ PARAMS='-A '\''Foo'\'' -B '\''Bar'\'''
+ ./test -A ''\''Foo'\''' -B ''\''Bar'\'''
Run Code Online (Sandbox Code Playgroud)
所以,问题是 - 如何将bash变量用作某些命令的命令行参数.变量类似于"-A'Foo'-B'Bar'"(完全用单引号)并且结果必须调用带有参数"-A'Foo'-B'Bar'"的程序"./test"这个:
./test -A 'Foo' -B 'Bar'
Run Code Online (Sandbox Code Playgroud)
谢谢!