multiholle
multiholle

Reputation: 3160

bash: treat quotes in quotes

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?


UPDATE Maybe my first example was not good enough. I looked at the answer of Rob Davis, but I couldn't apply the solution to my script. I tried to simplify my script to describe my problem better. This is the script:

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

Answers (2)

Rob Davis
Rob Davis

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

gpojd
gpojd

Reputation: 23055

Why quote it in the first place?

for i in test "foo bar baz"; do echo $i; done

Upvotes: 0

Related Questions