19.2. Globbing

Bash itself cannot recognize Regular Expressions. In scripts, commands and utilities, such as sed and awk, interpret RE's.

Bash does carry out filename expansion, a process known as "globbing", but this does not use the standard RE set. Instead, globbing recognizes and expands wildcards. Globbing interprets the standard wildcard characters, * and ?, character lists in square brackets, and certain other special characters (such as ^ for negating the sense of a match). There are some important limitations on wildcard characters in globbing, however. Strings containing * will not match filenames that start with a dot, as, for example, .bashrc. [1] Likewise, the ? has a different meaning in globbing than as part of an RE.

bash$ ls -l
total 2
 -rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 a.1
 -rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1
 -rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1
 -rw-rw-r--    1 bozo  bozo       466 Aug  6 17:48 t2.sh
 -rw-rw-r--    1 bozo  bozo       758 Jul 30 09:02 test1.txt

bash$ ls -l t?.sh
-rw-rw-r--    1 bozo  bozo       466 Aug  6 17:48 t2.sh

bash$ ls -l [ab]*
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 a.1
 -rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1

bash$ ls -l [a-c]*
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 a.1
 -rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1
 -rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1

bash$ ls -l [^ab]*
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1
 -rw-rw-r--    1 bozo  bozo       466 Aug  6 17:48 t2.sh
 -rw-rw-r--    1 bozo  bozo       758 Jul 30 09:02 test1.txt

bash$ ls -l {b*,c*,*est*}
-rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 b.1
 -rw-rw-r--    1 bozo  bozo         0 Aug  6 18:42 c.1
 -rw-rw-r--    1 bozo  bozo       758 Jul 30 09:02 test1.txt


bash$ echo *
a.1 b.1 c.1 t2.sh test1.txt

bash$ echo t*
t2.sh test1.txt
	      

Even an echo command performs wildcard expansion on filenames.

See also Example 10-4.

Notes

[1]

Filename expansion can match dotfiles, but only if the pattern explicitly includes the dot.
~/[.]bashrc    # Will not expand to ~/.bashrc
~/?bashrc      # Neither will this.
               # Wild cards and metacharacters will not expand to a dot in globbing.

~/.[b]ashrc    # Will expand to ~./bashrc
~/.ba?hrc      # Likewise.
~/.bashr*      # Likewise.

# Setting the "dotglob" option turns this off.

# Thanks, S.C.