Extended globbing

suggest change


$ mkdir globbing
$ cd globbing
$ mkdir -p folder/{sub,another}folder/content/deepfolder/
touch macy stacy tracy "file with space" folder/{sub,another}folder/content/deepfolder/file .hiddenfile
$ shopt -u nullglob
$ shopt -u failglob
$ shopt -u dotglob
$ shopt -u nocaseglob
$ shopt -u extglob
$ shopt -u globstar

Bash’s built-in extglob option can extend a glob’s matching capabilities

shopt -s extglob

The following sub-patterns comprise valid extended globs:

The pattern-list is a list of globs separated by |.

$ echo *([r-t])acy
stacy tracy

$ echo *([r-t]|m)acy
macy stacy tracy

$ echo ?([a-z])acy

The pattern-list itself can be another, nested extended glob. In the above example we have seen that we can match tracy and stacy with *(r-t). This extended glob itself can be used inside the negated extended glob !(pattern-list) in order to match macy

$ echo !(*([r-t]))acy

It matches anything that does not start with zero or more occurrences of the letters r, s and t, which leaves only macy as possible match.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents