Bash Command Line Expansion
External
Internal
Overview
Process
Each line read by bash from command line is interpreted as a pipeline: a sequence of commands separated by the pipe character (|).
Command processing starts by checking the first word of the command against an alias list, and if a match is found, the alias is replaced.
Then the result is parsed into tokens by using metacharacters as separators.
Command Line Expansion
The token are scanned to perform command line expansion, which are the following operations, performed in order:
Brace Expansion
Brace expansion is the process of expanding a set of comma-separated strings specified inside the braces into a space-separated list of strings. Each string from the list is prepended with a preamble, and appended with the postscript. Both the preamble and the postscript are optional. The left-to-right order of the strings within the braces is preserved in the expansion. Brace expansion occurs only if at least one comma exists inside the braces. Brace expansions can be nested.
echo KA{BO,DO}OM KABOOM KADOOM
"KA" is the preamble and "OM" is the postscript.
Brace expansion is turned on in interactive and noninteractive shells by default. It can be turned off with:
set +o braceexpand
Tilde Expansion
The tilde (~) is a special character when it appears at the start of a command line token. When detected on the first position, bash looks at the string of character up to the first slash or to the end of the word, if there is no slash, as a possible login name.
If this possible login name is null - the tilde appears as a word by itself or if it is immediately followed by a slash - bash substitutes the value of the HOME variable for the tilde. If the login name is valid and not null, bash substitutes the path of the home directory associated with that login name. If the login name is not null, but not valid, no substitution is made.
Parameter and Variable Expansion
A dollar sign ($) that is NOT followed by an open parenthesis initiates parameter or variable expansion.
Parameters include:
- Command line (positional) parameters
- Special parameters
Variables includes:
- User-created variables
- Keyword variables
Parameters and variables are not expanded if they are enclosed within single quotation marks or if the leading dollar sign is escaped (preceded with a backslash).
Arithmetic Expansion
Command Substitution
Word Splitting
Pathname Expansion
Pathname expansion is also called filename generation or globbing, and it is the process of interpreting ambiguous file references and substituting the appropriate list of filenames.
If the pathname expansion is unsuccessful - does not produce any hit - the original expression, including the special pathname expansion characters, are passed to the command.
Bash cannot recognize regular expressions but it can do filename expansion. The process is also known as globbing. The process recognizes and expands wild cards. Globbing interprets the following characters (for more details on metacharacters, see Regular Expressions Concepts - Metacharacters:
The Wild Card (*)
The wild card (*) matches every name in a given directory. Strings containing * will not match filenames that start with a dot.
The Question Mark (?)
The question mark (?) serves as a single-character "wild card" for filename expansion.
The Character Class [...]
The character class matches just one of the characters listed among square brackets. One of those characters must exist in the file name, on the specified position, for the name to match.
Negation (^)
^ negates the sense of match for the above.
Process Substitution
Quote Removal
After all command line expansion specified above are performed, bash proceeds with quote removal: it removes from the command line single quotation marks, double quotation marks and backslashes that are not a result of an expansion.