简体   繁体   English

Bash引号禁用转义

[英]Bash quotes disable escaping

I want to run some command, let's name it "test" from my bash script and put there some of params from bash variable. 我想运行一些命令,让我们从我的bash脚本命名为“test”,然后从bash变量中添加一些params。

My script: 我的剧本:

#!/bin/bash -x
PARAMS="-A 'Foo' -B 'Bar'"
./test $PARAMS

I've got: 我有:

+ PARAMS='-A '\''Foo'\'' -B '\''Bar'\'''
+ ./test -A ''\''Foo'\''' -B ''\''Bar'\'''

It's wrong! 这是不对的!

Another one case: 另一个案例:

#!/bin/bash -x
PARAMS='-A '"'"'Foo'"'"' -B '"'"'Bar'"'"
./test $PARAMS

Result is sad too: 结果是伤心过:

+ PARAMS='-A '\''Foo'\'' -B '\''Bar'\'''
+ ./test -A ''\''Foo'\''' -B ''\''Bar'\'''

So, question is – how can I use bash variable as command line arguments for some command. 所以,问题是 - 如何将bash变量用作某些命令的命令行参数。 Variable is something like "-A 'Foo' -B 'Bar'" (exactly with single-quotes) And result must be calling of program "./test" with arguments "-A 'Foo' -B 'Bar'" like this: 变量类似于“-A'Foo'-B'Bar'”(完全用单引号)并且结果必须调用带有参数“-A'Foo'-B'Bar'”的程序“./test”这个:

./test -A 'Foo' -B 'Bar'

Thanks! 谢谢!

It is safer to use BASH arrays for storing full or partial command lines like this: 使用BASH数组存储完整或部分命令行更安全,如下所示:

params=(-A 'Foo' -B 'Bar')

then call it as: 然后将其称为:

./test "${params[@]}"

which will be same as: 这将是:

./test -A 'Foo' -B 'Bar'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM