Re:Safe ways to do things in bash
by quietbritishjim on Monday May 14, 2018 @05:30PM
(#17069624)
Honest question: what happens when the variable has a quote in it? If FOO is ‹xyz"; rm -r *; "xyz› (where I've used ‹› as delimiters) then won't even "${FOO}" expand to multiple arguments/commands? Or does bash automatically escape the quotes in the situation?
[ Parent
| on HN ]
Quotes that arise from substitutions aren't considered to be quotes.
Unfortunately, the way GNU Bash handles this sort of requirement is to internally translate these protected quotes into some character that "nobody" would ever use, and then recover them later.
That characters code is none other than ASCII 1 (SOH/Ctrl-A). It's known by the preprocessor symbol CTLESC in the Bash sources.
jesus christ is nothing sacred
Both of those examples end up being fine. When quoted the entire variable is passed as a single WORD and the shell won't interpret the `;` as the end of the command. Even without quoting the variable it would be fine in this case because of where you've put the quotes, though you should still quote the variables. As a simple illustration take a look at the following:
function e() {
echo $#
}
$ x="hello world"
$ e $x
2
$ e "$x"
1