Wildcard
Wildcard is a "generalized" tool that helps you to operate in a pattern manner rather than a full name command / object. BASH has 2 forms of wildcard:
- Globbing
- Regular Expression
Here, we will focus on #1 - Globbing in terminal command. In this example, we're going to use a directory that has the following files:
holloway:demo$ ls
abc abcdefghijk abc.php abc.txt ABC.txt def mydoc.odt xyz.xml
abcdef abcdef.odt abctxt abc.TXT alphabet DEF xyz xyz.yaml
Globbing
Globbing is the symbolic wildcards. There are useful in command searches like searching a list of item with X or Y pattern.
*
Asterisk symbolizes anything else that matches regardless length. Example:
holloway:demo$ ls a*
abc abcdefghijk abc.php abc.txt alphabet
abcdef abcdef.odt abctxt abc.TXT
holloway:demo$ ls A*
ABC.txt
holloway:demo$ ls *.txt
abc.txt ABC.txt
holloway:demo$ ls *txt
abctxt abc.txt ABC.txt
holloway:demo$ ls a*b*
abc abcdefghijk abc.php abc.txt alphabet
abcdef abcdef.odt abctxt abc.TXT
holloway:demo$ ls *h*
abcdefghijk abc.php alphabet
?
Question mark is a length-specific symbolic wildcard regardless any character. Example:
holloway:demo$ ls D??
DEF
holloway:demo$ ls D?F
DEF
holloway:demo$ ls d??
def
holloway:demo$ ls *.???
abcdef.odt abc.php abc.txt abc.TXT ABC.txt mydoc.odt xyz.xml
holloway:demo$ ls *.????
xyz.yaml
{}
Listing set the search to have the options available within the list. Example:
holloway:demo$ ls abc.*
abc.php abc.txt abc.TXT
holloway:demo$ ls abc.{txt,TXT}
abc.txt abc.TXT
[]
Character set to search one character listed inside the set. Example:
holloway:demo$ ls a[a-z]c.txt
abc.txt
holloway:demo$ ls a[0-9]c.txt
ls: cannot access 'a[0-9]c.txt': No such file or directory
holloway:demo$ ls a[a-z]?.txt
abc.txt
holloway:demo$ ls a[[:alpha:]]?.txt
abc.txt
holloway:demo$ ls a[[:alnum:]]?.txt
abc.txt
holloway:demo$ ls a[[:blank:]]?.txt
ls: cannot access 'a[[:blank:]]?.txt': No such file or directory
holloway:demo$ ls a[[:digit:]]?.txt
ls: cannot access 'a[[:digit:]]?.txt': No such file or directory
holloway:demo$ ls a[[:lower:]]?.txt
abc.txt
holloway:demo$ ls [[:upper:]][[:upper:]][[:upper:]].txt
ABC.txt
holloway:demo$ ls [[:xdigit:]][[:xdigit:]][[:xdigit:]].txt
abc.txt ABC.txt
There are a list of regular expression ranges:
[[:alnum:]]
- A-Z, a-z, 0-9[[:alpha:]]
- A-Z, a-z[[:blank:]]
- Space, tab[[:cntl:]]
- ASCII characters 0-31 (non-printing control characters)[[:digit:]]
- 0-9[[:graph:]]
- ASCII characters 0-31 (non-printing control characters)[[:lower:]]
- a-z[[:print:]]
- ASCII characters 32-127 (printable characters)[[:punct:]]
- Punctuation (printable characters other than A-Z, a-z, 0-9)[[:space:]]
- Space, Tab, LF (10), VT (11), FF (12), CR (13)[[:upper:]]
- A-Z[[:xdigit:]]
- 0-9, A-F, a-f
Toggle Globing
Sometimes, you might want to toggle the glob settings in your terminal. You can do so by both BASH and terminal means.
Enable/Disable
To enable or disable Globbing, use:
- Disable:
$ set -o noglob
- Enable:
$ set +o noglob
Normally, we disable glob in situation where we really want the glob as a character, not a symbol. Example, naming a file with a glob character:
$ ls -l *d*.odt
-rw-rw-r-- 1 steve steve 505 Nov 13 10:13 abcdef.odt
-rw-rw-r-- 1 steve steve 355 Dec 20 09:17 mydoc.odt
$ touch *d*.odt
$ ls -l *d*.odt
-rw-rw-r-- 1 steve steve 505 Dec 22 11:51 abcdef.odt
-rw-rw-r-- 1 steve steve 355 Dec 22 11:51 mydoc.odt
$ set -o noglob
$ touch *d*.odt
$ set +o noglob
$ ls -l *d*.odt
-rw-rw-r-- 1 steve steve 505 Dec 22 11:51 abcdef.odt
-rw-rw-r-- 1 steve steve 0 Dec 22 11:52 *d*.odt
-rw-rw-r-- 1 steve steve 355 Dec 22 11:51 mydoc.odt
$
Shell Options Command
Sometimes, you might want to set option to use the glob in different way. You can do so by both BASH and terminal means using shopt
command. It has 5 common commands arguments:
-o
- restricts OPTNAMEs to those defined for the use withset -o
.-p
- print each shell options with an indication of its status-q
- quiet the output (suppress output)-s
- set (enable) the OPTNAME-u
- unset (disable) the OPTNAME
List All OPTNAMEs
To start, let's list all the available OPTNAMEs with shopt -p
:
holloway:demo$ shopt -p
shopt -u autocd
shopt -u cdable_vars
shopt -u cdspell
shopt -u checkhash
shopt -u checkjobs
shopt -s checkwinsize
shopt -s cmdhist
shopt -u compat31
shopt -u compat32
shopt -u compat40
shopt -u compat41
shopt -u compat42
shopt -u compat43
shopt -s complete_fullquote
shopt -u direxpand
shopt -u dirspell
shopt -u dotglob
shopt -u execfail
shopt -s expand_aliases
shopt -u extdebug
shopt -s extglob
shopt -s extquote
shopt -u failglob
shopt -s force_fignore
shopt -u globasciiranges
shopt -u globstar
shopt -u gnu_errfmt
shopt -s histappend
shopt -u histreedit
shopt -u histverify
shopt -u hostcomplete
shopt -u huponexit
shopt -u inherit_errexit
shopt -s interactive_comments
shopt -u lastpipe
shopt -u lithist
shopt -u login_shell
shopt -u mailwarn
shopt -u no_empty_cmd_completion
shopt -u nocaseglob
shopt -u nocasematch
shopt -u nullglob
shopt -s progcomp
shopt -s promptvars
shopt -u restricted_shell
shopt -u shift_verbose
shopt -s sourcepath
shopt -u xpg_echo
holloway:demo$
As you can see, those with -s
are set while those with -u
are unset.
View OPTNAME
If you want to view one particular option, simply supply the OPTNAME into it:
holloway:demo$ shopt -p xpg_echo
shopt -u xpg_echo
holloway:demo$
Set OPTNAME
Now that you have the list, you probably want to set some OPTNAME based on your preferences. You can set them using the shopt -s OPTNAME
command. If you did not provide OPTNAME, shopt will set everything. Example:
holloway:demo$ shopt -p nullglob
shopt -u nullglob
holloway:demo$ shopt -s nullglob
holloway:demo$ shopt -p nullglob
shopt -s nullglob
holloway:demo$
Unset OPTNAME
If you plan to unset some of the existing, you can proceed to use shopt -u OPTNAME
instead. If you did not provide OPTNAME, shopt will unset everything. Example:
holloway:demo$ shopt -p nullglob
shopt -s nullglob
holloway:demo$ shopt -u nullglob
holloway:demo$ shopt -p nullglob
shopt -u nullglob
holloway:demo$
Shell Options
As you may be curious, how can I study each of the options printed out for me? You can refer to the built-in manual. You probably not needing to toggle these options unless there is a special needs. Most of the stock configurations are well set. However, just for introduction, there are some you might find it interesting.
nullglob
This is usually default to unset. If the shell match no pattern, it returns an error number instead.
extglob
Usually set. However, some are not. This extends your glob options reach like AND or OR, one or many matches, etc.
?(pattern-list)
- Zero or one of the patterns
*(pattern-list)
- Zero or more of the patterns
+(pattern-list)
- One or more of the patterns
@(pattern-list)
- Exactly one of the patterns
!(pattern-list)
- Anything except one of the patterns
The most useful one would be !(...)
, @(...)
, and ?(...)
which are commonly used. Here's an example:
$ ls abc@(.txt|.php)
abc.php abc.txt
$ touch abc.txt.txt
$ ls abc@(.txt|.php)
abc.php abc.txt
$ ls abc+(.txt|.php)
abc.php abc.txt abc.txt.txt
$ ls abc*(.txt|.php)
abc abc.php abc.txt abc.txt.txt
$ ls abc@(.txt|.php)
abc.php abc.txt
$ ls abc!(.txt|.php)
abc abcdef abcdefghijk abcdef.odt abctxt abc.TXT abc.txt.txt
$ ls abc?(.)txt
abctxt abc.txt
$ ls abc?(def)
abc abcdef
$ ls abc?(def|.txt)
abc abcdef abc.txt
$
nocaseglob
Turn the glob pattern into case-insensitive or case-sensitive.
That's all about the wildcards. Feel free to proceed to the next section.