bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug 103756] kate: syntax highlighting error: bash and escaped quote


From: Eric Blake-1
Subject: Re: [Bug 103756] kate: syntax highlighting error: bash and escaped quotes
Date: Mon, 2 Apr 2007 16:17:26 -0700 (PDT)

> > 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.

> > 
> > x="`echo \"'\\\"\"`"    is valid
> 
> 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.
> 

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
the command:

`echo "'\""`

Then, according to `` rules, $, `, and \ are special (but not "), so
the subshell sees the command:

echo "'\""

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

`echo \"'\\\"\"`

and according to the rules of ``, the subshell sees:

echo \"'\\"\"

which indeed is a syntax error.

> 
> > The other way around:
> > 
> > x=`echo "'\""`          is valid

Valid.  The subshell sees:

echo "'\""

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 "'\""

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,
echo "'\""
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 $().

-- 
Eric Blake

-- 
View this message in context: 
http://www.nabble.com/Re%3A--Bug-103756--kate%3A-syntax-highlighting-error%3A-bash-and-escaped-quotes-tf3507272.html#a9800902
Sent from the Gnu - Bash mailing list archive at Nabble.com.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]