Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: quoting question



On Tue, Sep 12, 2023 at 11:46 AM Jim <linux.tech.guy@xxxxxxxxx> wrote:
>
> Zsh quoting at times makes me wonder. Do quoting? Don't do quoting?
> The following case has me scratching my head.  Can someone explain what is
> going on?

This doesn't directly have to do with quoting, rather it has to do
with ${param} expansion and brace expansion.  The short answer is you
should never have unquoted { } inside the ${...}, because the first }
encountered is usually going to be interpreted as a match to the ${
and not to any other { in the string.  Where double-quoting gets
involved is because inside a double-quoted string, none of the pairs
of { } have been tokenized, so the lexer can't distinguish which of
the two }} is the actual end of the expansion.

That's what happens here:

>   DT="${(%):-%D{%Y-%m-%d %H:%M:%S %Z}}"
>   print $DT
> 2023-09-12 13:21:40 CDT}    <--  why "}" when quoted

The string is read as ${(%):-%D{%Y-%m-%d %H:%M:%S %Z} followed by }
because the %D{ is quoted (not tokenized) so does not lexically match
with any closing }, so the first one ends the ${...}.

In this case:

>   DT=${(%):-%D{%Y-%m-%d %H:%M:%S %Z}}
>   print $DT
> 2023-09-12 13:21:40 CDT

The inner { } are tokenized and checked for whether they constitute an
{x,y,z} brace expression.  There's no comma, so they're left alone as
a matching pair, and the final } matches the opening ${.  You can see
this by inserting a comma:

DT=${(%):-%D{%Y-%m-%d, %H:%M:%S %Z}}

Now %H becomes the home directory instead of the hour, etc.

If you setopt ignorebraces you'll see that all five examples work the
same and always leave the extra } at the end.




Messages sorted by: Reverse Date, Date, Thread, Author