$_ CLI In Depth
Learning progress 0%
lesson terminal shell kernel

The Shell's Read, Parse & Execute Loop

6 min read Module 1: Terminal, Shell & Kernel Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:14

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.

BASH3 lines
touch 'a b'
touch a b
ls

The 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:

  1. Brace expansion creates several words from a pattern such as {a,b} or {1..3}. It is purely textual and happens first.
  2. Tilde expansion replaces ~ with the home directory, and ~user with that user home.
  3. Parameter and variable expansion replaces $name with its value.
  4. Arithmetic expansion replaces $((...)) with a number.
  5. Command substitution replaces $(...) with the output of a command.
  6. Word splitting breaks the results of the unquoted expansions into separate words wherever characters in IFS appear, by default spaces, tabs, and newlines.
  7. Pathname expansion matches words that contain *, ?, or [ against existing file names.
  8. 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.

BASH3 lines
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.

Warning

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.

BASH5 lines
x="a  b"
set -x
printf '[%s]\n' $x
printf '[%s]\n' "$x"
set +x

The 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.

BASH2 lines
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.

From keystrokes to a running program

Visualizer

Follow one line through the shell: read it, split it into words, expand them, find the command, run it, and collect its exit status.

1 / 8
read
tokenize
expand
look up
run
wait
$?
$ ls -l /tmp

Bash (through readline) reads one line from the terminal. Nothing has run yet: it is just the text ls -l /tmp.

type -a ls; ls -l /tmp; echo "exit status: $?"
Terminal lab · 3 steps · real Linux in your browser

Watch the shell split and expand

Use quoting, brace expansion and tilde expansion, and check exactly which words reach the program.

01
One word with a space
Create a single file whose name is my notes.txt, space included. Without quotes, the shell splits that into two words and creates two files.
Show hint for step 1
`touch 'my notes.txt'` keeps the space inside one word.
02
Brace expansion
Create day1.log through day5.log with a single short command, letting the shell generate the names.
Show hint for step 2
`touch day{1..5}.log` expands to five words before `touch` runs.
03
Expansion order
Brace expansion happens before tilde expansion. Save what the shell turns ~/{a,b} into: ``bash echo ~/{a,b} > expanded.txt ` Run it again with set -x first to watch the expanded command, and set +x` to stop.
Show hint for step 3
Your home directory in the lab is `/workspace`.
Check yourself Question 1 of 4
Concept Check First step of the shell operation recall

In the bash Shell Operation sequence, which step comes first for a line you type?

Recap & Key Takeaways

Bash runs each line through tokenizing, parsing, expansion, quote removal, redirection, execution, and waiting, in that order.
Single quotes keep text literal, double quotes keep words together but allow $ expansion, and unquoted results are split on IFS and globbed.
Expansion order is brace, tilde, parameter, arithmetic, command substitution, word splitting, pathname expansion, and then quote removal.
The set -x option prints each command after expansion, which is the quickest way to confirm the words a program will receive.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue