The loop
An interactive bash prints a prompt, reads a line, and performs the shell operation on it. The same steps happen to commands read from a script file and to commands passed with bash -c. The bash manual describes the operation in its Shell Operation section, and this unit follows that order: read the input, break it into words and operators, parse the words into commands, perform expansions, perform redirections, execute, and wait for the command to finish.
The steps are easier to follow when you keep one example in mind. Take the line printf '[%s]\n' $x with x set to two words. The rest of this unit explains why that line prints two lines and what the quotes change.
Tokenizing: words and operators
Bash breaks the line into tokens. Some tokens are words, and others are operators. The operators are the control operators |, ||, &, &&, ;, ;;, (, and ), plus the redirection operators such as <, >, and >>. Whitespace and the operator characters separate words, unless a character is quoted.
Quoting controls this step. A backslash makes the next character literal. Single quotes keep everything between them literal, so no expansion and no backslash escape happens inside them. Double quotes also keep the words together, but they still allow $ expansion, command substitution, and backslash escapes for a few characters. The quote characters themselves are removed later, in the quote removal step.
The effect of quoting on file names is the easiest place to see the difference. The first command creates one file whose name contains a space. The second creates two files, one named a and one named b.
touch 'a b'
touch a b
lsThe first touch call receives one argument, a b, because the single quotes kept the space inside the word. The second receives two arguments, because the unquoted space ended the first word. Bash makes this decision in the tokenizing step, before any program sees the arguments.
Parsing: from words to commands
After tokenizing, bash parses the tokens into a structure. A simple command is an optional list of variable assignments, followed by words and redirections. The first word is the command name, and the rest are its arguments. A pipeline joins commands with |. A list joins pipelines with ;, &, &&, or ||. A compound command is a construct such as if, for, while, case, a subshell in parentheses, or a group in braces.
Reserved words such as if, then, do, and done are recognized only where a command name could appear, and only when they are not quoted. So echo if prints the word if, because if is an argument there, not a keyword. This rule is why shell keywords do not interfere with ordinary words in most of your commands.
Expansions, in the order bash performs them
Expansion is the step that causes most surprises. Bash performs these expansions on each word, in the following order:
- Brace expansion creates several words from a pattern such as
{a,b}or{1..3}. It is purely textual and happens first. - Tilde expansion replaces
~with the home directory, and~userwith that user home. - Parameter and variable expansion replaces
$namewith its value. - Arithmetic expansion replaces
$((...))with a number. - Command substitution replaces
$(...)with the output of a command. - Word splitting breaks the results of the unquoted expansions into separate words wherever characters in
IFSappear, by default spaces, tabs, and newlines. - Pathname expansion matches words that contain
*,?, or[against existing file names. - Quote removal deletes the remaining quote characters, so that the program receives the text without them.
Redirections come after expansion. Bash opens the files named in > and similar operators, and it does so before the command runs. Only then does the command execute.
Quoting changes the words
Word splitting and pathname expansion apply only to unquoted results of expansion. That is why the value of x in the next example behaves differently depending on the quotes around $x. The variable holds two words separated by two spaces.
x="a b"
printf '[%s]\n' $x
printf '[%s]\n' "$x"The unquoted $x is split at the spaces, so printf receives two arguments, a and b, and it prints each one on its own line inside brackets. The quoted "$x" is one word, so printf prints [a b] once. The format string is quoted with single quotes, so the \n is passed to printf, which interprets it as a newline.
An unquoted variable is also subject to pathname expansion. If x is *, then echo $x lists the files in the current directory. Quote variables unless you want that behavior.
Hands-on: watch the expansion with set -x
The set -x option makes bash print each command after expansion, with a plus sign in front. This is the most direct way to see the words that a program actually receives. Run the block below and compare the trace with the output.
x="a b"
set -x
printf '[%s]\n' $x
printf '[%s]\n' "$x"
set +xThe trace for the unquoted call shows + printf '[%s]\n' a b, which is the two-word list that printf received. The trace for the quoted call shows + printf '[%s]\n' 'a b', with the value kept as one word. Bash adds the single quotes in the trace only to show you where word boundaries are. The program itself never sees them. The final set +x turns tracing off again.
Execute and wait
After the words and redirections are ready, bash executes the command. A builtin runs inside the shell process. An external program runs in a child process, as described in the first unit of this module. Bash then waits for the command to finish and stores its exit status in $?. The status is what makes && and || work. In false && echo yes, the echo never runs, because false reports 1.
false; echo $?
true; echo $?The first line prints 1 and the second prints 0. Most programs use more than these two values, and a later lesson in this module covers the full range. Pipelines report the status of their last command in $?, and the array PIPESTATUS keeps the status of each stage.