Re: Inconsistent handling of patterns with backslah (`\`) escaping

Chet Ramey <[email protected]> Mon, 1 Jun 2026 11:07:43 -0400
Newsgroups gmane.comp.shells.bash.bugs
Organization ITS, Case Western Reserve University
Message-ID <[email protected]>
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/
OpenPGP_signature.asc (application/pgp-signature, 203 B)
-----BEGIN PGP SIGNATURE-----

wmMEABEIACMWIQR8ATX7CIqvbGbGULm7WGnwZOp0qwUCah2gPwUDAAAAAAAKCRC7WGnwZOp0q92d
AJ9fMXQHjOHAs2Z3UPeuXX+5F6u2aQCdEnyAc5xk4DOUboGWoMaTI8b+c90=
=2aAd
-----END PGP SIGNATURE-----