On 5/31/26 10:32 AM, Andreas Schwab wrote:
On Mai 31 2026, Andrei Borzenkov wrote:

$ bar='\*'
$ ls $bar
ls: cannot access '\*': No such file or directory
$

Oops. Why it suddenly attempts to match *two* characters "\*" instead of
just one literal "*"?

Because '\*' is not a glob, so filename expansion is not performed.

The rationale in the POSIX standard (yes, I realize it's not normative) says:

"Patterns are matched against existing filenames and pathnames only when
the pattern contains a '*', '?' or '[' character that will be treated as
special. This prevents accidental removal of <backslash> characters in
variable expansions where generating a list of matching files is not
intended and a (usually oddly named) file with a matching name happens to
exist."

https://pubs.opengroup.org/onlinepubs/9799919799/xrat/V4_xcu_chap01.html#tag_23_02_14_04

Since the pattern contains a backslash, the following `*' will not "be
treated as special."

Most shells (the NetBSD sh is a notable exception) behave like bash does in
this case.

Chet

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

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to