Consider this bash
example:
TESTA="testing a \\\"string right here\\\""
echo $TESTA
# testing a \"string right here\"
The output is as expected: the first pair of backslashes resolved to a single backslash (\
), and the next escaped double quote \"
got expanded to a double quote ("
), so the final merged result is \"
.
Now this is the strange part for me:
TESTB=$(echo $TESTA)
echo $TESTB
# testing a \"string right here\"
Here I would have expected the contents of $TESTB
to be, indeed, testing a \"string right here\"
- but I expected that when that is printed out, the escaped double quote \"
would have expanded to just a double quote "
, and so I expected the printed result to be with unescaped double quotes, that is testing a "string right here"
; but that's not what I get echoed, I get the same escaped doublequotes as the contents...
So, is there a way to "expand" the $TESTA
so it ultimately contains unescaped double quotes? (I tried echo -e
which should "enable interpretation of backslash escapes", but it doesn't help)...
EDIT: it turns out, for this particular case, TESTB=$(eval echo "$TESTA") ; echo $TESTB
will indeed result with testing a "string right here"
-- but I don't want to eval
: if I'm building a command line string this way, using eval
will be trouble. So maybe I should better rephrase: how can I expand escaped double-quotes like this, as a result of a string operation of sorts (calling an extern program is OK, as long as the string itself is not eval
'd)