Shell expansion

The line is rewritten, in a fixed order, before anything runs

as typedecho ~/logs/*.log $HOME file{1..2}1 braceecho ~/logs/*.log $HOME file1 file2file{1..2}2 tildeecho /home/ana/logs/*.log $HOME file1 file2~3 variableecho /home/ana/logs/*.log /home/ana file1 file2$HOME4 word splitting[echo] [/home/ana/logs/*.log] [/home/ana] [file1] [file2]spaces5 pathname (glob)echo /home/ana/logs/app.log /home/ana/logs/db.log /home/ana file1 file2*.log6 quote removalthen the program starts with these wordsCommand substitution $(...) and arithmetic $((...)) run in step 3, left to right.The shell rewrites the line in a fixed order before the program sees a single word of it.echo never sees a star, a tilde or a dollar sign: by the time it runs, the shell has replaced them with file names and values.A star that matches nothing is left as it is, which is why rm *.bak in an empty folder complains about a file called *.bak.Knowing the order explains most surprises: a variable holding a space becomes two words at step 4 unless it is quoted.

Expansion replaces braces, tildes, variables, substitutions and globs before the program starts.

Quoting

Quotes choose which of those rewrites happen

In a home directory holding notes.txt, with HOME=/home/ana:You typeecho printsWhat happenedecho $HOME *.txt/home/ana notes.txteverything expandedecho "$HOME *.txt"/home/ana *.txtvariables only, one wordecho '$HOME *.txt'$HOME *.txtnothing expandedecho \$HOME$HOMEone character escapedDouble quotes keep variables working and stop splitting and globbing. Single quotes stop everything.Quote every variable you did not write yourself: rm $file deletes two files if the name holds a space, and rm "$file" one.Use single quotes for text that must reach a program untouched, such as an awk program or a regular expression:awk '{print $1}' gives awk its own $1, which the shell would otherwise have replaced with nothing.

Double quotes keep variables and make one word; single quotes pass text through untouched.

Variables

How do you name a value once and use it in many commands?

$ name=web011$ echo "Backing up $name"2Backing up web01$ today=$(date +%F)3$ echo "$name-$today.tar.gz"web01-2026-11-16.tar.gz$ echo "$nmae"4$ echo "$name_old"5$ echo "${name}_old"web01_old1name=value sets a variableno spaces around the = sign2$name reads itthe shell puts the value in its place3$(command) is its outputhere, today’s date4A misspelt name is emptyno error, just nothing5Braces end the name$name_old is another variableA variable is a name for a piece of text. The shell replaces $name with the value before the command runs.With spaces, name = web01 is read as a command called name with two arguments, and fails. A variable set this waybelongs to this shell only: programs you start do not see it. The next page shows what export changes.

A shell variable is set with name=value and read with $name; command substitution, $(...), stores a command’s output.

Environment variables

Exported variables are copied into every program you start

bash (PID 2140)PATH=/usr/local/bin:/usr/bin:/binHOME=/home/anaLANG=en_GB.UTF-8colour=bluea shell variableexport EDITOR=vimnow in the environmentpurple: exported, copied to every childstartschild: python3 deploy.py (PID 2188)PATH=/usr/local/bin:/usr/bin:/binHOME=/home/anaLANG=en_GB.UTF-8colournot here: never exportedEDITOR=vimA child process gets a copy of the exported variables, and nothing it changes flows back.This is how every program learns where you live, which language you read and where to look for commands. Because the copy isone way, a script cannot change its caller’s directory or variables; to change the current shell, source the file instead:source ~/.bashrc, or . ~/.bashrc. env lists the environment; set lists every shell variable too.

A child process gets a copy of the environment; plain shell variables, and changes made by the child, do not cross.

Startup files · Going deeper

Which files bash reads when it starts

Login shell: SSH, console, su -/etc/profile/etc/profile.d/*.sh~/.bash_profile (or ~/.profile) which usually runs ~/.bashrcInteractive non-login: a new tab, bash/etc/bash.bashrc (Ubuntu)/etc/bashrc (RHEL, from ~/.bashrc)~/.bashrcScripts run by cron or systemd read none of these: give them full paths and set what they need.Put environment variables in ~/.bash_profile and aliases and prompt settings in ~/.bashrc.For every user on the machine, add a file to /etc/profile.d/ rather than editing /etc/profile, so package updates do not overwrite it.Changes apply to new shells only; source the file to apply it now. Per-user limits such as the number of open files are notset here but by PAM, in /etc/security/limits.conf, and shown with ulimit -a.

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

ls -lis ls ...an alias?alias ll='ls -l'a function?defined in this shella builtin?cd, echo, export, typeno: search PATH,left to rightPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin/usr/local/sbinno ls here/usr/local/binno ls here/usr/sbinno ls here/usr/binls found: runs this/sbinnever reached/binnever reached$ type -a lsls is /usr/bin/ls ls is /bin/lsA name without a slash is looked up: alias, function, builtin, then each PATH directory in order.The first match wins, so a program in /usr/local/bin hides one with the same name in /usr/bin. A name with a slash, such as./deploy.sh, skips the search, which is why a script in the current directory needs ./ in front: . is not in PATH, on purpose.type tells you which one will run; on Ubuntu 24.04 and RHEL 10, /bin and /sbin are links to /usr/bin and /usr/sbin.

Bash checks aliases, functions and builtins, then each PATH directory from left to right.

Exit status

Every command ends with a number, and 0 means success

$ ls /var/log/syslog/var/log/syslog$ echo $?0$ ls /nopels: cannot access '/nope': No such file or directory$ echo $?2mkdir /srv/app && cp app.conf /srv/app&& runs the second only if the first returned 0ping -c1 db01 || echo "db01 is down"|| runs the second only if the first failed0 success · 1 general failure · 2 misuse126 not executable · 127 not found · 130 Ctrl-CEvery command ends by returning a number from 0 to 255, and only 0 means success.The shell keeps the last one in $?. It is how scripts, cron, systemd and CI pipelines decide whether something worked:they never read the error message, only this number. && and || chain commands on it, and if in a script tests it.Values above 128 mean the process was killed by a signal: 128 plus the signal number, so Ctrl-C (signal 2) gives 130.

The exit status in $? is how scripts and tools decide whether a command worked; && and || act on it.

History and aliases

How do experienced people type so little?

Key or commandWhat it does↑ ↓step back and forward through the commands you have typedCtrl-Rsearch that history: type a few letters of an old commandTabcomplete a command, file or directory name; twice lists the choices!!the whole last command, as in sudo !!Ctrl-A Ctrl-Ejump to the start and the end of the linealias ll='ls -alF'a short name for a command you type oftentype llll is aliased to `ls -alF'The shell remembers what you typed and completes what you start. Let it do the typing.Tab is also a check: if a path will not complete, it does not exist as typed. history lists the saved commands with numbers;bash keeps them in ~/.bash_history when the session ends. An alias lasts for the session: to keep one, put it in ~/.bashrc,the startup file covered under the environment page.

History recalls earlier commands, Tab completion finishes names, and an alias gives a long command a short name.

Lab

Ask the shell what it will do before it does it

$ type cd ls ll1cd is a shell builtinls is /usr/bin/lsll is aliased to `ls -alF'$ echo ~/logs/*.log2/home/ana/logs/app.log /home/ana/logs/db.log$ name="my file.txt"; touch "$name"; ls3'my file.txt' logs notes.txt$ ls /nope 2>/dev/null || echo "status $?"4status 2$ man -k "change.*password"5chage (1) - change user password expiry informationpasswd (1) - change user password...1Which kind of commandbuiltin, program on PATH, or alias2Expansion before runningecho prints what the shell made3Quote what can hold spacesone file, not two4Read the exit statusused by || and by every script5Find a page by its purposethen man 8 chpasswdLab: ask the shell what it will do, then do it.Eight short steps on your own Linux machine, from reading the prompt to making a setting survive a new shell. Press the down arrow.

type, echo, quotes, $? and man -k answer most questions about a command line before you run it for real.

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.

Steps

  1. 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.
  2. 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).
  3. 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'.
  4. 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.
  5. 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.
  6. 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.
  7. 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!.
  8. 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.

Lesson complete

You finished “How the shell works”

Nicely done.

Open in a new tab