BЈовић
BЈовић

Reputation: 64253

What [...] in a bash script stands for?

I am reading this tutorial, and I encountered that bash script uses [...] as a wild card character. So what exactly [...] stands in a bash script?

Upvotes: 3

Views: 489

Answers (3)

Matteo Italia
Matteo Italia

Reputation: 126867

It's a regex-style character matching syntax; from the Bash Reference Manual, §3.5.8.1 (Pattern Matching):

[...] Matches any one of the enclosed characters. A pair of characters separated by a hyphen denotes a range expression; any character that sorts between those two characters, inclusive, using the current locale's collating sequence and character set, is matched. If the first character following the ‘[’ is a ‘!’ or a ‘^’ then any character not enclosed is matched. A ‘−’ may be matched by including it as the first or last character in the set. A ‘]’ may be matched by including it as the first character in the set. The sorting order of characters in range expressions is determined by the current locale and the value of the LC_COLLATE shell variable, if set.

For example, in the default C locale, ‘[a-dx-z]’ is equivalent to ‘[abcdxyz]’. Many locales sort characters in dictionary order, and in these locales ‘[a-dx-z]’ is typically not equivalent to ‘[abcdxyz]’; it might be equivalent to ‘[aBbCcDdxXyYz]’, for example. To obtain the traditional interpretation of ranges in bracket expressions, you can force the use of the C locale by setting the LC_COLLATE or LC_ALL environment variable to the value ‘C’.

Within ‘[’ and ‘]’, character classes can be specified using the syntax [:class:], where class is one of the following classes defined in the posix standard:

          alnum   alpha   ascii   blank   cntrl   digit   graph   lower
          print   punct   space   upper   word    xdigit

A character class matches any character belonging to that class. The word character class matches letters, digits, and the character ‘_’.

Within ‘[’ and ‘]’, an equivalence class can be specified using the syntax [=c=], which matches all characters with the same collation weight (as defined by the current locale) as the character c.

Within ‘[’ and ‘]’, the syntax [.symbol.] matches the collating symbol symbol.

(emphasis added to the most common usage patterns)

Upvotes: 6

Benoit
Benoit

Reputation: 79205

Actually, what is a wildcard is [abc] for example. It matches one of the three letters.

Upvotes: 1

Benoit Thiery
Benoit Thiery

Reputation: 6387

It is used in the tutorial to speak about regular expressions in addition to globbing ('*' and '?'). For example [a-z] regular expression will match one lowercase character.

Upvotes: 5

Related Questions