开发者

Quoting in the function arguments error

开发者 https://www.devze.com 2023-04-03 10:24 出处:网络
How should be fixed command variable to get a correct behavior? #!/bin/bash function f ( ) { echo \"$2\" }

How should be fixed command variable to get a correct behavior?

#!/bin/bash

function f ( )
{
    echo "$2"
}
command="f --option=\"One Tw开发者_开发知识库o Three\" --another_option=\"Four Five Six\""
$command

f --option="One Two Three" --another_option="Four Five Six"

First calling is wrong, second - right

$> ./test.sh 
Two
--another_option=Four Five Six


BASH FAQ entry #50: "I'm trying to put a command in a variable, but the complex cases always fail!"

TL;DR: Use an array.

command=(f --option="One Two Three" --another_option="Four Five Six")
"${command[@]}"


You cannot fix the variable. But you can:

eval $command
0

精彩评论

暂无评论...
验证码 换一张
取 消