help-bash
[Top][All Lists]
Advanced

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

Re: Why echo without quotes need to glob the file


From: Greg Wooledge
Subject: Re: Why echo without quotes need to glob the file
Date: Tue, 13 Dec 2022 10:02:51 -0500

On Tue, Dec 13, 2022 at 08:28:25AM -0600, G. Branden Robinson wrote:
> "1" lies between "-" and "h".  It seems that in most shells (I tried
> bash, ksh93, dash, yash, and zsh) a leading dash in a glob bracket
> expression is treated as a range bound.  This may be mandated by POSIX
> but I'll leave that question for another contributor.

Without delving into POSIX specifications, I'll simply point out that
if the hyphen appears as either the first or last character in the
square brackets, it's treated as a literal hyphen.  If it's not the
first or last character, then it's a range indicator.

unicorn:~$ mkdir /tmp/empty && cd "$_"
unicorn:/tmp/empty$ touch a b c d e f g h
unicorn:/tmp/empty$ echo [a-c]
a b c
unicorn:/tmp/empty$ echo [ac-]
a c

In the first case, "a-c" is a range, so the glob matches any of the
characters from a to c.

In the second case, "ac-" is not a range.  It's simply the three
characters a, c and -, and it matches any one of those characters.

This is useful when you want to match something like "any letter or digit
or the underscore or the hyphen".  In strict POSIX conformance, we
would write that as

[[:alnum:]_-]

The hyphen must be first or last, in order to be matched literally.

In the OP's particular case, there are *two* hyphens.  The leading one
is treated as a literal character, and the second one is the range
indicator.  Therefore, the first hyphen is the start of the range.



reply via email to

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