Quoting in the function arguments error

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

#!/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"

First calling is wrong, second - right

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

Upvotes: 0

Views: 101

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798456

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[@]}"

Upvotes: 2

Karoly Horvath
Karoly Horvath

Reputation: 96258

You cannot fix the variable. But you can:

eval $command

Upvotes: 0

Related Questions