Chapter Three

The Shell

How Bash reads a command line, the expansions it runs before any program starts, and the control over input, output, variables, and jobs that turns a prompt into a tool.

6 topics

The shell is the program that turns a line of text into running processes. On Debian and Ubuntu the interactive default is Bash, and most of what people call "the command line" is really Bash applying a fixed sequence of rules — splitting words, expanding patterns and variables, wiring up input and output — before a single binary ever executes. The commands you run are the small part; the shell's processing around them is the part that bites.

This chapter takes Bash apart in the order it actually works. It starts with how a command line is parsed and what a builtin is, moves through the expansions and globbing that rewrite your words behind your back, then covers redirection and pipes, the quoting that decides which of those expansions fire, the variables and environment that processes inherit, and finally job control and history. Get these six right and most "why did that not do what I typed" surprises disappear.

How bash turns a line into a command
Readone line
Expandbrace, var, command, glob
Locatebuiltin / PATH
Executefork + exec

Topics in This Chapter

Topic 12
Bash Fundamentals
How Bash parses a command line into words, the difference between builtins, functions, aliases, and external binaries, and the exit status that every command returns. The model the rest of the chapter builds on.
ShellParsing
Topic 13
Expansion and Globbing
The order Bash runs brace, tilde, parameter, command, and arithmetic expansion, then word splitting and pathname globbing. Why *.txt is expanded by the shell, not the command, and what happens when nothing matches.
ExpansionGlobbing
Topic 14
Pipes and Redirection
The three standard streams, redirecting stdout and stderr with >, 2>, and &>, and chaining processes through pipes. Where 2>&1 has to go and why pipeline exit status surprises people.
StreamsRedirection
Topic 15
Quoting and Escaping
Single quotes that suppress every expansion, double quotes that keep variable and command substitution, and the backslash for one character. Why unquoted variables break on spaces and how quoting prevents word splitting.
QuotingEscaping
Topic 16
Variables and the Environment
Shell variables versus exported environment variables, how child processes inherit the environment, and where PATH, HOME, and the rest come from. Why a variable set in a subshell never reaches its parent.
VariablesEnvironment
Topic 17
Job Control and History
Foreground and background jobs, suspending with Ctrl-Z, and jobs, bg, fg, and disown. Plus command history expansion and why a backgrounded job still dies on logout unless you detach it.
JobsHistory