.. you'll get into trouble when `$foo` contains special characters. At
first, you might think of doing this:
sh -c "do something with \"$foo\""
Now, `$foo` is properly quoted, right? Nope. It works with spaces and
the asterisk, but *quotes* inside of `$foo` still break it:
$ foo='hello world'
$ sh -c "echo \"$foo\""
hello world
$ foo='hello * world'
$ sh -c "echo \"$foo\""
hello * world
$ foo='hello " world'
$ sh -c "echo \"$foo\""
sh: -c: line 1: unexpected EOF while looking for matching `"'
sh: -c: line 2: syntax error: unexpected end of file
In GNU Bash, you could do this:
$ foo='hello " world * and friends'
$ printf -v foo_quoted '%q' "$foo"
$ sh -c "echo $foo_quoted"
hello " world * and friends
But, well, that's Bash and not portable. I think a better way is to pass
`$foo` as an argument to the snippet, like so:
$ foo='hello " world * and friends'
$ sh -c 'echo "$1"' _ "$foo"
hello " world * and friends