4.3. Pattern matching using Bash features

4.3.1. Character ranges

Apart from grep and regular expressions, there's a good deal of pattern matching that you can do directly in the shell, without having to use an external program.

As you already know, the asterisk (*) and the question mark (?) match any string or any single character, respectively. Quote these special characters to match them literally:


cathy ~> touch "*" cathy ~> ls "*" app3.html chap_01.html chap_02.html chap_03.html chap_04.html chap_05.html chap_06.html chap_07.html chap_08.html chap_09.html chap_10.html chap_11.html chap_12.html f32.html glossary.html gloss.html images index.html intro_01.html intro_02.html intro_03.html intro_04.html intro_05.html intro_06.html intro_07.html intro_08.html intro_09.html intro_10.html sect_01_01.html sect_01_02.html sect_01_03.html sect_01_04.html sect_01_05.html sect_01_06.html sect_01_07.html sect_02_01.html sect_02_02.html sect_02_03.html sect_02_05.html sect_02_06.html sect_03_01.html sect_03_02.html sect_03_03.html sect_03_04.html sect_03_05.html sect_03_06.html sect_03_07.html sect_03_08.html sect_04_01.html sect_04_02.html sect_04_03.html sect_04_04.html sect_04_05.html sect_05_01.html sect_05_02.html sect_05_03.html sect_05_04.html sect_05_05.html sect_06_01.html sect_06_02.html sect_06_03.html sect_06_04.html sect_06_05.html sect_07_01.html sect_07_02.html sect_07_03.html sect_07_04.html sect_07_05.html sect_08_01.html sect_08_02.html sect_08_03.html sect_08_04.html sect_09_01.html sect_09_02.html sect_09_03.html sect_09_04.html sect_09_05.html sect_09_06.html sect_09_07.html sect_09_08.html sect_09_09.html sect_10_01.html sect_10_02.html sect_10_03.html sect_10_04.html sect_10_05.html sect_11_01.html sect_11_02.html sect_11_03.html sect_11_04.html sect_12_01.html sect_12_02.html sect_12_03.html sect_12_04.html x7253.html x7379.html 

But you can also use the square braces to match any enclosed character or range of characters, if pairs of characters are separated by a hyphen. An example:


cathy ~> ls -ld [a-cx-z]* drwxr-xr-x 2 cathy cathy 4096 Jul 20 2002 app-defaults/ drwxrwxr-x 4 cathy cathy 4096 May 25 2002 arabic/ drwxrwxr-x 2 cathy cathy 4096 Mar 4 18:30 bin/ drwxr-xr-x 7 cathy cathy 4096 Sep 2 2001 crossover/ drwxrwxr-x 3 cathy cathy 4096 Mar 22 2002 xml/ 

This lists all files in cathy's home directory, starting with "a", "b", "c", "x", "y" or "z".

If the first character within the braces is "!" or "^", any character not enclosed will be matched. To match the dash ("-"), include it as the first or last character in the set. The sorting depends on the current locale and of the value of the LC_COLLATE variable, if it is set. Mind that other locales might interpret "[a-cx-z]" as "[aBbCcXxYyZz]" if sorting is done in dictionary order. If you want to be sure to have the traditional interpretation of ranges, force this behavior by setting LC_COLLATE or LC_ALL to "C".

4.3.2. Character classes

Character classes can be specified within the square braces, using the syntax [:CLASS:], where CLASS is defined in the POSIX standard and has one of the values

"alnum", "alpha", "ascii", "blank", "cntrl", "digit", "graph", "lower", "print", "punct", "space", "upper", "word" or "xdigit".

Some examples:


cathy ~> ls -ld [[:digit:]]* drwxrwxr-x 2 cathy cathy 4096 Apr 20 13:45 2/ cathy ~> ls -ld [[:upper:]]* drwxrwxr-- 3 cathy cathy 4096 Sep 30 2001 Nautilus/ drwxrwxr-x 4 cathy cathy 4096 Jul 11 2002 OpenOffice.org1.0/ -rw-rw-r-- 1 cathy cathy 997376 Apr 18 15:39 Schedule.sdc 

When the extglob shell option is enabled (using the shopt built-in), several extended pattern matching operators are recognized. Read more in the Bash info pages, section Basic shell features->Shell Expansions->Filename Expansion->Pattern Matching.