File Globbing: Path Name Expansion
The Bash shell has a path name-matching capability historically called
globbing, abbreviated from the "global command"
file path expansion program of early UNIX.
The Bash globbing feature, commonly called pattern matching
or "wildcards", makes managing large numbers of files easier. Using
meta-characters that "expand" to match file and
path names being sought, commands perform on a focused set of files at once.
A sample set of files is useful to demonstrate expansion.
Pattern Matching
Globbing is a shell command-parsing operation that expands a wildcard pattern into a list of matching path names. Command-line meta-characters are replaced by the match list prior to command execution. Patterns, especially square-bracketed character classes, that do not return matches display the original pattern request as literal text. The following are common meta-characters and pattern classes.Pattern | Matches |
---|---|
* | Any string of zero or more characters. |
? | Any single character. |
~ | The current user's home directory. |
~username
|
User username 's home directory.
|
~+ | The current working directory. |
~- | The previous working directory. |
[abc... ]
|
Any one character in the enclosed class. |
[!abc... ]
|
Any one character not in the enclosed class. |
[^abc... ]
|
Any one character not in the enclosed class. |
[[:alpha:]] | Any alphabetic character.(1) |
[[:lower:]] | Any lower-case character.(1) |
[[:upper:]] | Any upper-case character.(1) |
[[:alnum:]] | Any alphabetic character or digit.(1) |
[[:punct:]] | Any printable character not a space or alphanumeric.(1) |
[[:digit:]] |
Any digit, 0-9 .(1)
|
[[:space:]] | Any one whitespace character; may include tabs, newline, or carriage returns, and form feeds as well as space.(1) |
Note | (1)pre-set POSIX character class; adjusts for current locale. |
First, simple pattern matches using[student@desktopX ~]$
mkdir glob; cd glob
[student@desktopX glob]$
touch alfa bravo charlie delta echo able baker cast dog easy
[student@desktopX glob]$
ls
able alfa baker bravo cast charlie delta dog easy echo[student@desktopX glob]$
*
and ?
.
[student@desktopX glob]$
ls a*
able alfa[student@desktopX glob]$
ls *a*
able alfa baker bravo cast charlie delta easy[student@desktopX glob]$
ls [ac]*
able alfa cast charlie[student@desktopX glob]$
ls ????
able alfa cast easy echo[student@desktopX glob]$
ls ?????
baker bravo delta[student@desktopX glob]$
Tilde and Brace Expansion
Tilde Expansion
The tilde character (~
), when followed by a slash delimiter,
matches the current user's home directory. When followed by a
string of characters up to a slash, it will be interpreted as a username,
if one matches. If no username matches, then an actual tilde followed
by the string of characters will be returned.
[student@desktopX glob]$
ls ~/glob
Command Substitution
Command substitution allows the output of a command to replace the command itself.
Command substitution occurs when a command is enclosed with a beginning
dollar sign and parenthesis,
$(command
)
,
or with backticks, `command
`
.
The form with backticks is older, and has two disadvantages: 1) it can be easy to
visually confuse backticks with single quote marks, and 2) backticks cannot be
nested inside backticks. The $(command
)
form can nest multiple command expansions inside each other.
[student@desktopX glob]$
echo Today is `date +%A`.
Today is Wednesday.[student@desktopX glob]$
echo The time is $(date +%M) minutes past $(date +%l%p).
The time is 26 minutes past 11AM.[student@desktopX glob]$
[student@desktopX glob]$
echo ~/glob
/home/student/glob
[student@desktopX glob]$
Brace Expansion
Brace expansion is used to generate discretionary strings of characters. Braces contain a comma-separated list of strings, or a sequence expression. The result includes the text preceding or following the brace definition. Brace expansions may be nested, one inside another.[student@desktopX glob]$
echo {Sunday,Monday,Tuesday,Wednesday}.log
Sunday.log Monday.log Tuesday.log Wednesday.log
[student@desktopX glob]$
echo file{1..3}.txt
file1.txt file2.txt file3.txt
[student@desktopX glob]$
echo file{a..c}.txt
filea.txt fileb.txt filec.txt
[student@desktopX glob]$
echo file{a,b}{1,2}.txt
filea1.txt filea2.txt fileb1.txt fileb2.txt
[student@desktopX glob]$
echo file{a{1,2},b,c}.txt
filea1.txt filea2.txt fileb.txt filec.txt
[student@desktopX glob]$
Protecting Arguments from Expansion
Many characters have special meaning in the Bash shell. To ignore meta-character special meanings, quoting and escaping are used to protect them from shell expansion. The backslash (\
) is an escape character in Bash,
protecting the single following character from special interpretation.
To protect longer character strings, single ('
) or
double quotes ("
) are used to enclose strings.
Use double quotation marks to suppress globbing and shell expansion, but still allow command and variable substitution. Variable substitution is conceptually identical to command substitution, but may use optional brace syntax.
Use single quotation marks to interpret all text literally. Observe the difference, on both screen and keyboard, between the single quote ([student@desktopX glob]$
host=$(hostname -s); echo $host
desktopX[student@desktopX glob]$
echo "***** hostname is ${host} *****"
***** hostname is desktopX *****[student@desktopX glob]$
echo Your username variable is \$USER.
Your username variable is $USER.[student@desktopX glob]$
'
) and the command substitution backtick (`
).
Besides suppressing globbing and shell expansion, quotations direct the shell
to additionally suppress command and variable substitution.
The question mark is a meta-character that also needed protection from expansion.
[student@desktopX glob]$
echo "Will variable $host evaluate to $(hostname -s)?"
Will variable desktopX evaluate to desktopX?[student@desktopX glob]$
echo 'Will variable $host evaluate to $(hostname -s)?'
Will variable $host evaluate to $(hostname -s)?[student@desktopX glob]$
Comments
Post a Comment
thank you for visiting :)