[英]Bash joining array
Is there a way to join (with proper quoting and escaping) a bash array such that it gives me a string suitable for passing to bash -c
? 有没有一种方法可以连接(使用正确的引号和转义符)bash数组,从而为我提供适合传递给
bash -c
的字符串?
Reason : I want to write a function foo
, such that when I do this: 原因 :我想编写一个函数
foo
,这样当我这样做时:
foo app "hello world.txt"
it'll do this: 它将这样做:
sudo -u myuser ENVVAR=somevalue bash -c "perl app \"hello world.txt\""
bash -c
takes the first argument as the command string, and assigns the remaining arguments to positional arguments starting from $0
. bash -c
取第一参数作为命令串,而其余的参数分配给从起始位置参数$0
。 So, you don't need to quote anything, just pass the arguments: 因此,您无需引用任何内容,只需传递参数即可:
function foo () {
sudo -u myuser ENVVAR=somevalue bash -c 'perl "$0" "$@"' "$@"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.