Shell expansion
The line is rewritten, in a fixed order, before anything runs
Expansion replaces braces, tildes, variables, substitutions and globs before the program starts.
Here is the shell at work on one line. Braces expand first, into a list. A tilde becomes your home directory. Variables are replaced by their values, and command substitutions by their output. Then the line is split into words on spaces. Then any word with a star or question mark is matched against file names. Finally the quotes are removed, and only now does the program start, with a list of plain words. Echo never saw a star or a dollar sign.
Quoting
Quotes choose which of those rewrites happen
Double quotes keep variables and make one word; single quotes pass text through untouched.
Quotes are how you control expansion. Unquoted, everything expands: the variable and the star. In double quotes, the variable still expands, but the star stays a star, and the whole thing is one word. In single quotes, nothing expands at all. A backslash protects one character. The habit to build: always put variables in double quotes, so a value with a space stays one word.
Variables
How do you name a value once and use it in many commands?
A shell variable is set with name=value and read with $name; command substitution, $(...), stores a command’s output.
The expansion page showed the shell replacing dollar HOME. You can make names of your own. Name, equals sign, value, with no spaces. A dollar sign in front reads it back, and the shell puts the value into the line. Dollar and parentheses run a command and use its output as the value. Misspell a name and you get nothing: no error, an empty string. And where the name runs straight into more text, braces show where it ends.
Environment variables
Exported variables are copied into every program you start
A child process gets a copy of the environment; plain shell variables, and changes made by the child, do not cross.
Each shell holds variables. Some are exported: PATH, HOME, LANG, and any you export yourself. When the shell starts a program, those are copied into it. A plain shell variable stays behind. And the copy only goes one way: nothing the child changes comes back. That is why running a script cannot change your current directory, and why you source a file when you want its settings in the shell you are in.
Startup files · Going deeper
Which files bash reads when it starts
Login shells read /etc/profile and ~/.bash_profile; interactive shells read ~/.bashrc. Cron and systemd read neither.
Command search and PATH
A bare name is searched for, and the first match wins
Bash checks aliases, functions and builtins, then each PATH directory from left to right.
How does bash know where ls lives? A name with no slash is looked up. First it checks for an alias, a function, or a builtin like cd. If none matches, it walks the PATH variable, one directory at a time, from left to right, and runs the first ls it finds. Later directories are never reached. type shows you the answer before you run anything. And because the current directory is not on the PATH, a script beside you needs dot slash in front of it.
Exit status
Every command ends with a number, and 0 means success
The exit status in $? is how scripts and tools decide whether a command worked; && and || act on it.
Programs report success with a number, not with words. After a command, dollar question mark holds it: zero for success, anything else for failure. Double ampersand runs the next command only if the first succeeded. Double bar runs it only if the first failed. A few values are worth knowing: one hundred and twenty-seven means the command was not found, and anything above one hundred and twenty-eight means a signal killed it. Every script, cron job and pipeline you will write depends on this number.
History and aliases
How do experienced people type so little?
History recalls earlier commands, Tab completion finishes names, and an alias gives a long command a short name.
Watch someone experienced at a terminal and they seem to type almost nothing. The shell keeps a history: the arrows walk through it, and Control R searches it. Tab completes whatever you have started, and it doubles as a check that the name exists. Two exclamation marks stand for the whole last command, which is handy when you forgot sudo. And for a command you type every day, an alias gives it a short name.
Lab
Ask the shell what it will do before it does it
type, echo, quotes, $? and man -k answer most questions about a command line before you run it for real.
Time to use it. In this lab you classify commands with type, watch expansion with echo, protect a file name with quotes, read exit statuses and search the manual. Press the down arrow for the steps.
Lab · Going deeper
Twenty minutes on a fresh Linux machine
Use any Linux you can break: a cloud VM, a local VM, or WSL. Use Ubuntu Server 24.04 LTS, or AlmaLinux or Rocky Linux 10 if you are aiming at the Red Hat exam.
Use any Linux you can break: a cloud VM, a local VM, or WSL. Use Ubuntu Server 24.04 LTS, or AlmaLinux or Rocky Linux 10 if you are aiming at the Red Hat exam. Steps. Read the prompt. Run whoami, hostname and pwd and match each answer to a part of your prompt. Run sudo -i, see the $ become #, and exit at once. Classify commands. Run type cd ls ll history and type -a echo. Which of them would still work with PATH empty? Try it in a subshell: (PATH=; cd /tmp && echo ok; ls). Watch expansion. mkdir -p ~/lab/logs; touch ~/lab/logs/{app,db,web}.log; then compare echo ~/lab/logs/*.log, echo "~/lab/logs/*.log" and echo '$HOME'. Break word splitting. name="my file.txt"; touch $name; ls. Then rm my file.txt and redo it with "$name". Explain why the first made two files. Use exit statuses. Run grep -q root /etc/passwd; echo $?, then the same for a user who does not exist. Chain mkdir -p /tmp/x && echo made. Find help three ways. ls --help | head; help cd; man 5 passwd. Use man -k to find the command that shows who is logged in. Edit a file with vim. vim ~/lab/notes.txt: i, type a line, Esc, :wq. Reopen it, delete the line with dd, quit without saving with :q!. Make it stick. Add export EDITOR=vim and alias ll='ls -alF' to the right startup files, open a new shell and prove both work. Check: type ll says it is an alias, a file name with a space survives as one file, and you can save and quit vim without looking it up.
Steps
- Read the prompt. Run whoami, hostname and pwd and match each answer to a part of your prompt. Run sudo -i, see the $ become #, and exit at once.
- Classify commands. Run type cd ls ll history and type -a echo. Which of them would still work with PATH empty? Try it in a subshell: (PATH=; cd /tmp && echo ok; ls).
- Watch expansion. mkdir -p ~/lab/logs; touch ~/lab/logs/{app,db,web}.log; then compare echo ~/lab/logs/*.log, echo "~/lab/logs/*.log" and echo '$HOME'.
- Break word splitting. name="my file.txt"; touch $name; ls. Then rm my file.txt and redo it with "$name". Explain why the first made two files.
- Use exit statuses. Run grep -q root /etc/passwd; echo $?, then the same for a user who does not exist. Chain mkdir -p /tmp/x && echo made.
- Find help three ways. ls --help | head; help cd; man 5 passwd. Use man -k to find the command that shows who is logged in.
- Edit a file with vim. vim ~/lab/notes.txt: i, type a line, Esc, :wq. Reopen it, delete the line with dd, quit without saving with :q!.
- Make it stick. Add export EDITOR=vim and alias ll='ls -alF' to the right startup files, open a new shell and prove both work.
Lesson complete
You finished “How the shell works”
Nicely done.