[Top][All Lists]

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

Re: Backslash missing in brace expansion

From: Chet Ramey
Subject: Re: Backslash missing in brace expansion
Date: Fri, 6 Dec 2019 09:12:28 -0800
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.1

On 12/5/19 8:53 PM, Martin Schulte wrote:

>> It's an unquoted backslash, which is removed by quote removal when the
>> words are expanded. Look at the extra space between `[' and `]'; that's
>> the null argument resulting from the unquoted backslash.
> Yes - sure. But then I'm wondering why the unquoted backtick doesn't
> start command substitution:
> $ echo {Z..a}
> Z [  ] ^ _ ` a
> $ echo Z [ \ ] ^ _ ` a

It's a reasonable question. The short answer is that brace expansion
takes place after parsing and results in multiple words.

Look at it this way. When the parser is reading a simple command, it
applies certain rules (matching quotes, metacharacter recognition, etc.)
and produces a list of words, which the shell expands and executes. Brace
expansion is similar in that it takes a single word and produces a list
of words which are then run through word expansion, but it does not apply
the same rules as the parser (or any rules, really).

What you end up with is a list of words: [, \, ], ^, _, `. a .

Each of those words is expanded as usual. When you get to the word that
consists of the single character "`", the expansion code checks whether
or not it's a valid command substitution, and, finding it's a single "`"
instead, leaves it unchanged.

The single backslash, as I explained earlier, is removed, resulting in a
null word after expansion.

This is an example of how you can use brace expansion to produce words
the parser would never have passed as valid.

``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    address@hidden    http://tiswww.cwru.edu/~chet/

reply via email to

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