Reputation: 3160
I isolated a problem in my script to this small example. That's what I get:
$ cmd="test \"foo bar baz\""
$ for i in $cmd; do echo $i; done
test
"foo
bar
baz"
And that's what I expected:
$ cmd="test \"foo bar baz\""
$ for i in $cmd; do echo $i; done
test
"foo bar baz"
How can I change my code to get the expected result?
#!/bin/bash
function foo {
echo $1
echo $2
}
bar="b c"
baz="a \"$bar\""
foo $baz
This it the expected output compared to the output of the script:
expected script
a a
"b c" "b
Upvotes: 2
Views: 194
Reputation: 15772
First, you're asking the double-quotes around foo bar baz
to do two things simultaneously, and they can't. You want them to group the three words together, and you want them to appear as literals. So you'll need to introduce another pair.
Second, parsing happens when you set cmd
, and cmd
is set to a single string. You want to work with it as individual elements, so one solution is to use an array variable. sh
has an array called @
, but since you're using bash you can just set your cmd
variable to be an array.
Also, to preserve spacing within an element, it's a good idea to put double quotes around $i
. You'd see why if you put more than one space between foo
and bar
.
$ cmd=(test "\"foo bar baz\"")
$ for i in "${cmd[@]}"; do echo "$i"; done
See this question for more details on the special "$@"
or "${cmd[@]}"
parsing feature of sh and bash, respectively.
Update
Applying this idea to the update in your question, try setting baz
and calling foo
like this:
$ baz=(a "\"$bar\"")
$ foo "${baz[@]}"
Upvotes: 2
Reputation: 23055
Why quote it in the first place?
for i in test "foo bar baz"; do echo $i; done
Upvotes: 0