Current bash.xml accepts (a), but not (b). The single qoute is
the start of string highlight. It's also not just the backtick
block, but the backtick block inside of a double quote string
block - and vice versa.
First, be aware that `` in bash 3.1 was buggy, so you should
upgrade to bash 3.2 rather than trying to figure out how the
broken 3.1 parsing worked.
My bash highlighter thinks this is invalid. So does my brain. However
bash seems OK with it. Why, I wonder? Maybe I am not clear on the
expansion rules in this instance.
x="`echo \"'\\\"\"`" is valid
This is valid. According to POSIX, the double quoted string is terminated
by the matching unquoted double quote, and within that string, the ``
string is executed on the contents of the `` with the \ quotes before any
$, `, ", \, and newline removed. So the result is that you are executing
Then, according to `` rules, $, `, and \ are special (but not "), so
the subshell sees the command:
which is valid, and results in '" being assigned to x.
x=`echo \"'\\\"\"` isn't
Ditto, except bash also (correctly IMO) doesn't like it.
Here, there is no surrounding "" to strip leading \ quotes
special to "". So you are executing the command
and according to the rules of ``, the subshell sees:
which indeed is a syntax error.
The other way around:
x=`echo "'\""` is valid
Valid. The subshell sees:
and x is assigned '"
x="`echo "'\""`" isn't
but both are highlighted as valid.
Invalid. The "" ends at the first unquoted ", but that meant
that you had an unpaired `, so bash is allowed to reject this.
The difference was that by adding "" around ``, you have
to also add \ escapes around characters special to "".
That's odd, because all of the following are valid:
echo `echo "'\""`
Valid. The inner echo results in the three characters '"<newline>,
then the `` strips the newline, and since this was the expansion
of ``, no further quoting is needed and the outer echo results
in '" again.
x="$(echo "'\"")" <-- this should be syntactically equivalent?!
$() has different rules than ``. Inside $(), any valid script
is allowed, and by itself,
is a valid script. And inside "$()", the rules are explicit that the
contents of the $() are not subject to normal "" escapes, but
that the closing ) is found by a recursive parse.
...so this feels like a bug in bash.
No, bash is correct. `` and $() are different, and the
rules for "``" are different from "$()". And if it weren't
for the fact that Solaris /bin/sh still doesn't understand
$() which hampers its portable use, I would gladly ditch
`` for the nicer semantics of $().