0

应该如何固定command变量以获得正确的行为?

#!/bin/bash

function f ( )
{
    echo "$2"
}
command="f --option=\"One Two Three\" --another_option=\"Four Five Six\""
$command

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

第一次调用是错误的,第二次是正确的

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

2 回答 2

2

BASH 常见问题解答条目 #50:“我正在尝试将命令放入变量中,但复杂的情况总是失败!”

TL;DR:使用数组。

command=(f --option="One Two Three" --another_option="Four Five Six")
"${command[@]}"
于 2011-09-08T21:23:26.423 回答
0

您无法修复变量。但是你可以:

eval $command
于 2011-09-08T21:18:28.813 回答