All posts
Bash

The Complete Bash Scripting Course: A Written Study Guide

65 min read

A chapter-by-chapter written companion to Dave Eddy's free Complete Bash Scripting Course on YouTube, with examples and explanations for every topic, from the terminal basics to traps, printf, globbing and debugging.

The Complete Bash Scripting Course: A Written Study Guide

Credits: This guide is a companion to The Complete Bash Scripting Course, created and presented by Dave Eddy of You Suck at Programming. All credit for the course goes to him.

More links, including how to support him, are in the credits at the bottom.

If you want to learn Bash properly, there is one resource I keep recommending: The Complete Bash Scripting Course by Dave Eddy of You Suck at Programming. It is completely free, it is a little over seven hours long, and it takes you from "what is a terminal?" all the way to traps, named pipes, printf tricks and debugging.

The course is on YouTube as a playlist of 62 short videos, one per section:

Videos are great for learning, but when you are writing a script you want something you can search and copy from. So this post is my written study guide for the course. It follows the same chapters and sections, and for each topic it gives a short explanation plus examples you can paste into a terminal.

A few things to know before we start:

  • This is an independent companion, not a transcript. The explanations and code examples are my own, written to match the topics of each section. Watch the videos to hear it explained by the person who made the course.
  • The section names (like 03-02 Functions) match the course's chapter list, so you can jump between this post and the playlist.
  • All credit for the course goes to Dave Eddy. See the credits at the bottom.

Before you start

Which shell, which Bash?

You need a terminal and Bash. On Linux you almost certainly have both. On macOS there are two things to know:

  1. The default shell on macOS is zsh, not Bash. You can still run Bash by typing bash.
  2. The /bin/bash that ships with macOS is the very old version 3.2. Several features in this guide need a newer Bash (4.x or 5.x). Install one with Homebrew (brew install bash) and start your scripts with #!/usr/bin/env bash, which picks the first Bash found in your PATH.

Check your version:

bash --version
echo "$BASH_VERSION"

I mark features that need a newer Bash as (Bash 4+) and so on.

Course map

ChapterTopicSections
00Introduction00-00
01Terminal basicsTerminal and Finder, basic file manipulation, hidden files, searching in files, paging files
02Know your toolsMan pages, programs and commands, basic variables, vim crash course, file permissions
03Finally scriptingUser input, functions, conditionals, for loops, input and output, recap
04Data and expansionCase statements, arrays, IFS, command substitution, arithmetic, process substitution
05Text toolscut and tr, sed, awk and grep, find
06Script plumbingArguments, pipe status, timing commands
07Code structureSourcing, curlies vs parens, return vs output
08ExpansionsParameter expansion, array expansion
09GlobbingBasic globbing, extended globbing, glob shell options
10BracesBrace expansion, braces and globbing, numeric brace expansion
11Formatting and matchingprintf, date formatting, regular expressions, mapfile
12TestsBrackets vs test, special strings
13Signals and pipesTrap signals, named pipes
14Terminal outputColour output, cursor commands, is a TTY
15Your shellPS1, customizing Bash, readline shortcuts
16PitfallsPitfall: ls, aliases with arguments, pitfall: string length
17Fork bombsForkbomb
BonusDebuggingBonus debugging session

00 Introduction

A terminal is the window. A shell is the program inside it that reads your commands and runs them. Bash is one very common shell (others are zsh, fish and dash). A shell script is simply a text file full of commands that the shell runs for you, top to bottom.

The smallest useful script:

#!/usr/bin/env bash
echo "Hello from Bash $BASH_VERSION"

Save it as hello.sh, make it executable and run it:

chmod +x hello.sh
./hello.sh

You will see exactly what each of those lines does in chapter 03.

01 Terminal basics

01-00 Terminal and Finder

Your terminal is always "in" a directory, the current working directory. Moving around is the first skill.

pwd                 # print the current directory
ls                  # list files
ls -lah             # long format, all files, human-readable sizes
cd ~/Documents      # go to a directory
cd ..               # up one level
cd -                # back to the previous directory
cd                  # no argument: go home

A few path shortcuts worth memorising:

ShortcutMeaning
/The root of the filesystem (or the separator between folders)
~Your home directory
.The current directory
..The parent directory
-With cd: the previous directory

An absolute path starts with / (for example /etc/hosts). A relative path starts from where you are (for example src/main.sh).

The graphical file manager (Finder on macOS) and the terminal show the same files. You can hop between them:

open .          # macOS: open the current folder in Finder
xdg-open .      # Linux desktop: open in the default file manager

Two habits that will save you hours: press Tab to autocomplete file names, and press the up arrow to bring back previous commands.

01-01 Basic file manipulation

mkdir -p projects/demo/src          # -p creates parent folders and doesn't complain if they exist
cd projects/demo
touch README.md src/main.sh         # create empty files
echo "# Demo" > README.md           # > overwrites the file
echo "second line" >> README.md     # >> appends to the file
cat README.md                       # print the file
 
cp README.md README.bak             # copy a file
cp -r src src-copy                  # copy a folder (-r = recursive)
mv README.bak notes.md              # move or rename
rm notes.md                         # delete a file
rm -r src-copy                      # delete a folder and everything in it

There is no trash can in the terminal: rm is permanent. Use rm -i to be asked first, and be extra careful with rm -r. Never put an unchecked variable in front of a slash, as in rm -rf "$dir/". If dir is empty, that becomes rm -rf /. A safer form (explained in chapter 08) is rm -rf "${dir:?}/", which aborts when dir is empty.

01-02 Hidden files

On Unix, any file whose name starts with a dot is hidden from normal listings. That is the whole trick.

ls          # does not show dotfiles
ls -a       # shows everything, including . and ..
ls -A       # everything except . and ..

Hidden files are where configuration lives: ~/.bashrc, ~/.gitconfig, ~/.ssh/, .gitignore. To hide a file, rename it with a leading dot:

mv secret.txt .secret.txt

01-03 Searching in files

grep prints the lines that match a pattern.

grep "error" app.log              # lines containing "error"
grep -i "error" app.log           # case-insensitive
grep -n "error" app.log           # show line numbers
grep -v "debug" app.log           # invert: lines NOT matching
grep -w "log" app.log             # whole word only
grep -c "error" app.log           # count matching lines
grep -r "TODO" ./src              # search a folder recursively
grep -rl "TODO" ./src             # only list the file names
grep -C 2 "panic" app.log         # show 2 lines of context around each match
grep -E "error|warning" app.log   # extended regex: this OR that

Combine it with pipes to filter any command's output:

ps aux | grep nginx
history | grep ssh

01-04 Paging files

When output is longer than your screen, use a pager.

less /var/log/syslog          # scroll a file
head -n 20 app.log            # first 20 lines
tail -n 20 app.log            # last 20 lines
tail -f app.log               # follow a file as it grows (Ctrl-C to stop)
some-command | less           # page any output

Inside less:

KeyAction
Space / bPage down / page up
g / GTop / bottom
/word then nSearch forward, next match
?word then nSearch backward
qQuit

If the output has colour codes, use less -R.

02 Know your tools

02-00 Man pages

Almost every command ships with a manual.

man ls              # the manual for ls (q to quit)
man 5 crontab       # section 5 (file formats) instead of section 1 (commands)
man -k "copy files" # search manuals by keyword (same as apropos)
ls --help           # most commands print a short summary too
help cd             # Bash's own built-ins use "help", not man

Man pages open in less, so everything from 01-04 applies: search with /, quit with q. Reading the SYNOPSIS and OPTIONS parts first is usually enough.

02-01 Programs and commands

When you type a word, what does Bash actually run? It can be several different things:

  • an alias (a nickname),
  • a function (code defined in your shell),
  • a builtin (part of Bash itself, such as cd),
  • an external program (a file somewhere in PATH, such as /bin/ls).
type cd         # cd is a shell builtin
type ls         # ls is /bin/ls  (or an alias)
type -a echo    # show ALL matches, in lookup order
command -v git  # print the path if it exists, exit non-zero if not
echo "$PATH"    # the folders Bash searches, separated by colons

command -v is the portable way to check whether a program is installed:

if ! command -v jq >/dev/null 2>&1; then
  echo "please install jq" >&2
  exit 1
fi

02-02 Basic variables

name="Robin"            # NO spaces around =
echo "Hello, $name"     # double quotes expand variables
echo 'Hello, $name'     # single quotes do not
echo "${name}_backup"   # braces mark where the name ends
unset name              # remove the variable

Two rules that prevent a huge number of bugs:

  1. No spaces around =. name = "Robin" tries to run a command called name.
  2. Quote your expansions. "$var" keeps the value as one word, even if it contains spaces.

Shell variables are local to the current shell. To pass one to programs you start, export it:

export EDITOR=vim
readonly VERSION="1.0"      # cannot be changed later
env | grep EDITOR           # list environment variables

Useful built-in variables: $HOME, $USER, $PWD, $PATH, $RANDOM, $$ (the shell's process ID) and $? (the exit status of the last command).

02-03 vim crash course

You will end up in vim eventually (for git commit, crontab -e, ...). The one idea to understand: vim has modes.

  • Normal mode (the default): keys are commands.
  • Insert mode: keys type text. Enter with i, leave with Esc.
  • Command mode: type : for commands like save and quit.
vim file.txt     open a file
 
i                insert before the cursor      a   append after the cursor
o                open a new line below         Esc go back to normal mode
:w               save                          :q  quit
:wq  or  ZZ      save and quit                 :q! quit without saving
 
h j k l          move left, down, up, right    gg / G  top / bottom of file
x                delete a character            dd  delete (cut) a line
yy               copy a line                   p   paste
u                undo                          Ctrl-r  redo
/word            search, then n for next       :%s/old/new/g  replace everywhere
:set number      show line numbers

If you just want to avoid vim, set a friendlier editor: export EDITOR=nano.

02-04 File permissions

ls -l script.sh
# -rwxr-xr-- 1 robin staff 120 Nov 17 10:00 script.sh

Read that first column in four parts:

-      rwx      r-x      r--
type   owner    group    others

Each triplet is read, write, execute. As numbers: read is 4, write is 2, execute is 1, and you add them up per group.

ModeMeaningTypical use
755rwxr-xr-xScripts and programs
644rw-r--r--Normal files
600rw-------Private keys, secrets
700rwx------Private folders
chmod +x script.sh          # add execute for everyone
chmod 755 script.sh         # numeric form
chmod u+x,g-w file          # symbolic: user +x, group -w
chmod -R 644 docs/          # recursive (careful with folders!)
chown robin:staff file      # change owner and group (usually needs sudo)
umask                       # the default-permission mask for new files

For folders, x means "allowed to enter it", so folders normally need 755, not 644.

03 Finally scripting

03-00 Finally scripting

A script is a text file. Three ingredients make it a proper one:

  1. A shebang on the first line telling the system which interpreter to use.
  2. Execute permission (chmod +x).
  3. A command to run it: ./script.sh.
#!/usr/bin/env bash
# greet.sh: a tiny script
 
echo "Script name: $0"
echo "Hello, $USER!"
chmod +x greet.sh
./greet.sh              # run it directly (uses the shebang)
bash greet.sh           # or hand it to Bash explicitly (no chmod needed)
echo $?                 # the exit status of the last command: 0 = success

Why #!/usr/bin/env bash instead of #!/bin/bash? env finds Bash through your PATH, which works on systems where Bash is not in /bin and lets you use a newer Bash you installed yourself.

Every command ends with an exit status: 0 means success and anything from 1 to 255 means failure. You set your script's status with exit:

exit 0      # success
exit 1      # generic failure

03-01 User input

Use read to ask a question and $1, $2, ... for arguments given on the command line.

#!/usr/bin/env bash
 
read -r -p "What is your name? " name
echo "Nice to meet you, $name."
 
read -r -s -p "Password: " pass     # -s hides what you type
echo
read -r -t 5 -p "Quick, answer in 5s: " answer || echo "too slow"
  • -r stops read from treating backslashes specially. Use it basically always.
  • -p prints a prompt, -s is silent, -t is a timeout in seconds.

Arguments:

#!/usr/bin/env bash
# ./hi.sh Robin
echo "First argument: $1"
echo "All arguments: $@"
echo "Number of arguments: $#"

03-02 Functions

#!/usr/bin/env bash
 
greet() {
  local name=${1:-world}      # local keeps the variable inside the function
  echo "Hello, $name!"
}
 
greet               # Hello, world!
greet Robin         # Hello, Robin!

Things to know:

  • Arguments inside a function are $1, $2, $@, just like a script.
  • Always declare helper variables with local, otherwise they leak into the rest of the script.
  • A function must be defined before it is called (Bash reads top to bottom).
  • return sets an exit status (0 to 255). To produce a value, print it. See 07-02.

03-03 Conditionals

#!/usr/bin/env bash
 
read -r -p "Enter a number: " n
 
if [[ $n -gt 10 ]]; then
  echo "bigger than 10"
elif [[ $n -eq 10 ]]; then
  echo "exactly 10"
else
  echo "smaller than 10"
fi

The most useful tests inside [[ ... ]]:

TestTrue when
-e pathThe path exists
-f path / -d pathIt is a regular file / a directory
-r, -w, -x pathIt is readable / writable / executable
-z str / -n strThe string is empty / not empty
a == b, a != bStrings are equal / not equal
n -eq, -ne, -lt, -le, -gt, -geNumber comparisons

Chain commands by their exit status with && (and) and || (or):

mkdir -p build && cd build && echo "ready"
[[ -f config.txt ]] || echo "config missing" >&2

if actually tests the exit status of any command, not just [[ ]]:

if grep -q "error" app.log; then
  echo "found errors"
fi

03-04 For loops

# Over a list of words
for fruit in apple banana cherry; do
  echo "I like $fruit"
done
 
# Over files (a glob)
for file in *.txt; do
  echo "Found $file"
done
 
# A range
for i in {1..5}; do
  echo "Count: $i"
done
 
# C-style, for when you need a variable limit
n=3
for ((i = 1; i <= n; i++)); do
  echo "Round $i"
done

Also useful: while loops, break and continue.

# Read a file line by line
while IFS= read -r line; do
  echo "> $line"
done < input.txt

03-05 Input / Output

Every program has three standard streams:

NumberNameDefault
0stdinKeyboard
1stdoutTerminal (normal output)
2stderrTerminal (errors)
command > out.txt             # stdout to a file (overwrite)
command >> out.txt            # stdout appended
command 2> errors.txt         # stderr to a file
command > all.txt 2>&1        # both into one file (order matters!)
command &> all.txt            # shorthand for the line above
command > /dev/null 2>&1      # throw everything away
command < input.txt           # read stdin from a file
echo "oops" >&2               # print to stderr (use this for error messages)

Pipes connect one command's stdout to the next command's stdin:

cat access.log | cut -d' ' -f1 | sort | uniq -c | sort -rn | head

A here-document feeds several lines as input, and a here-string feeds one string:

cat <<EOF
Dear $USER,
This text is passed to cat on stdin.
EOF
 
grep "b" <<< "abc"            # here-string

03-06 Chapter 3 recap

Here is a small script that combines input, functions, conditionals, loops and redirection:

#!/usr/bin/env bash
# backup.sh: copy all .txt files from a folder into a dated backup folder
 
usage() {
  echo "usage: $0 <source-dir>" >&2
  exit 1
}
 
[[ $# -eq 1 ]] || usage
src=$1
[[ -d $src ]] || { echo "not a directory: $src" >&2; exit 1; }
 
dest="backup-$(date +%F)"
mkdir -p "$dest"
 
count=0
for file in "$src"/*.txt; do
  [[ -e $file ]] || continue        # skip if the glob matched nothing
  cp -- "$file" "$dest/"
  count=$((count + 1))
done
 
echo "copied $count file(s) to $dest"

04 Data and expansion

04-00 Case statements

case is a tidy replacement for a long if/elif chain when you compare one value against many patterns.

#!/usr/bin/env bash
 
read -r -p "Continue? [y/n] " answer
 
case ${answer,,} in          # ,, lowercases the value (Bash 4+)
  y|yes)
    echo "going on"
    ;;
  n|no)
    echo "stopping"
    exit 0
    ;;
  *)
    echo "please answer y or n" >&2
    exit 1
    ;;
esac
  • Each branch ends with ;;.
  • | means "or" between patterns, and * is the catch-all default.
  • Patterns are globs, not regexes (see chapter 09), so *.txt) and [0-9]) work.

04-01 Indexed arrays

fruits=(apple banana "dragon fruit")
 
echo "${fruits[0]}"             # apple (indexes start at 0)
echo "${fruits[@]}"             # all elements
echo "${#fruits[@]}"            # how many elements: 3
fruits+=(mango)                 # append
fruits[1]="blueberry"           # change one
echo "${fruits[@]:1:2}"         # slice: 2 elements starting at index 1
 
for fruit in "${fruits[@]}"; do   # ALWAYS quote "${array[@]}"
  echo "- $fruit"
done
 
for i in "${!fruits[@]}"; do      # loop over the indexes
  echo "$i: ${fruits[i]}"
done

The quotes in "${fruits[@]}" matter: they keep dragon fruit as one element instead of splitting it into two.

04-02 Associative arrays

Arrays with string keys (a "dictionary" or "map"). Bash 4+.

declare -A user=(
  [name]="Robin"
  [city]="Johnstown"
)
 
user[shell]="bash"                 # add a key
echo "${user[name]}"               # Robin
 
for key in "${!user[@]}"; do       # loop over the keys
  echo "$key = ${user[$key]}"
done
 
[[ -v user[city] ]] && echo "city is set"   # does a key exist?
unset 'user[city]'                          # remove a key

You must use declare -A first. Without it, Bash treats the keys as numbers.

04-03 IFS variable

IFS (Internal Field Separator) controls how Bash splits text into words. Its default is space, tab and newline.

line="alice,30,paris"
 
IFS=, read -r name age city <<< "$line"
echo "$name is $age and lives in $city"
 
IFS=, read -ra parts <<< "$line"        # -a stores into an array
echo "${parts[1]}"                      # 30
 
IFS=: read -ra dirs <<< "$PATH"         # split PATH into an array
printf '%s\n' "${dirs[@]}"

Setting IFS= right before read affects only that one command. Avoid changing IFS globally. If you do, restore it or do it inside a function with local IFS.

A related trick: "${array[*]}" joins elements using the first character of IFS.

parts=(a b c)
( IFS=-; echo "${parts[*]}" )           # a-b-c

04-04 Command substitution

Run a command and use its output as a value with $(...).

today=$(date +%F)
echo "Today is $today"
 
files=$(ls | wc -l)
echo "There are $files entries"
 
echo "Parent folder: $(basename "$(dirname "$PWD")")"   # nesting works cleanly
  • Prefer $(...) over the older backticks. It nests and reads better.
  • Trailing newlines are removed from the captured output.
  • Quote it, "$(cmd)", when the output may contain spaces.

04-05 Arithmetic expression

Bash does integer maths only.

a=7; b=3
echo $((a + b))         # 10
echo $((a * b))         # 21
echo $((a / b))         # 2 (integer division)
echo $((a % b))         # 1 (remainder)
echo $((a ** 2))        # 49
 
(( a++ ))               # increment, no output
(( a > b )) && echo "a is bigger"
 
x=$(( (a + b) * 2 ))    # use the result

$(( )) produces a value you can use, while (( )) is a command whose exit status is true when the result is non-zero (handy in if).

For decimals, use a tool: awk 'BEGIN { print 7 / 3 }' or echo "scale=2; 7 / 3" | bc.

04-06 Process substitution

<(command) makes a command's output look like a file.

# Compare the output of two commands
diff <(sort a.txt) <(sort b.txt)
 
# Feed a loop WITHOUT losing variables to a subshell
count=0
while read -r line; do
  count=$((count + 1))
done < <(grep -r "TODO" .)
echo "$count TODOs"

The second example is the important one. If you wrote grep ... | while ..., the while loop would run in a subshell and count would be lost when it ends. Process substitution keeps the loop in your main shell.

04-07 Chapter 4 recap

A word-frequency counter using an associative array, process substitution and parameter expansion (Bash 4+):

#!/usr/bin/env bash
# wordcount.sh <file>: print the 10 most common words
 
declare -A counts
 
while read -r word; do
  [[ -n $word ]] || continue
  word=${word,,}                                  # lowercase
  counts[$word]=$(( ${counts[$word]:-0} + 1 ))    # default to 0 if unset
done < <(tr -cs '[:alpha:]' '\n' < "$1")          # one word per line
 
for word in "${!counts[@]}"; do
  printf '%5d %s\n' "${counts[$word]}" "$word"
done | sort -rn | head -10

05 Text tools

05-00 cut and tr

cut extracts columns. tr translates or deletes characters.

echo "alice:x:1000:1000" | cut -d: -f1        # alice (field 1, delimiter colon)
cut -d: -f1,3 /etc/passwd                     # fields 1 and 3
echo "hello world" | cut -c1-5                # hello (characters 1 to 5)
 
echo "hello" | tr a-z A-Z                     # HELLO
echo "a  b   c" | tr -s ' '                   # a b c (squeeze repeated spaces)
echo "a1b2c3" | tr -d '0-9'                   # abc (delete digits)
tr -d '\r' < windows.txt > unix.txt           # strip Windows line endings

05-01 sed, awk, and grep

grep finds lines (see 01-03). sed edits streams of text. awk processes text field by field.

# sed: substitute
sed 's/foo/bar/' file.txt           # first foo on each line
sed 's/foo/bar/g' file.txt          # every foo
sed -n '2,4p' file.txt              # print only lines 2 to 4
sed '/^#/d' file.txt                # delete comment lines
sed -i.bak 's/foo/bar/g' file.txt   # edit IN PLACE, keep file.txt.bak

The -i.bak form works on both GNU (Linux) and BSD (macOS) sed. Plain -i without a suffix is the part that differs between them.

# awk: columns are $1, $2, ... and $0 is the whole line
awk '{ print $1 }' access.log                  # first column
awk -F: '{ print $1, $7 }' /etc/passwd         # custom separator
awk '$3 > 100' data.txt                        # lines where column 3 is greater than 100
awk '{ sum += $1 } END { print sum }' nums.txt # total of column 1
awk 'NR == 1 { next } { print }' data.csv      # skip the header line

A handy rule of thumb: use grep to select lines, sed to change text, and awk to calculate with columns.

05-02 Find command

find walks a folder tree and filters by name, type, age, size and more.

find . -name "*.log"                    # by name (quote the pattern!)
find . -type f -name "*.md"             # only regular files
find . -type d -name node_modules       # only directories
find . -maxdepth 1 -type f              # don't recurse
find . -mtime -1                        # modified in the last day
find . -mtime +30                       # older than 30 days
find . -size +100M                      # bigger than 100 MB
find . -name "*.tmp" -delete            # delete matches
 
find . -name "*.sh" -exec chmod +x {} + # run a command on the matches
find . -type f -print0 | xargs -0 wc -l # safe with spaces in names

-exec cmd {} + passes many files to one command invocation (fast). -print0 and xargs -0 separate names with a NUL character, so even file names containing spaces or newlines survive.

06 Script plumbing

06-00 Bash arguments

#!/usr/bin/env bash
# args.sh a "b c" d
 
echo "count: $#"
echo "script: $0"
echo "first: $1"
 
for arg in "$@"; do          # "$@" keeps each argument as its own word
  echo "arg: $arg"
done
 
shift                        # drop $1, so $2 becomes $1
echo "after shift: $1"
  • "$@" is almost always what you want: each argument stays separate, even with spaces.
  • "$*" joins them into one string.
  • shift removes the first argument, great for loops that consume arguments.

For real options like -v or -n name, use getopts:

#!/usr/bin/env bash
verbose=0
name="world"
 
while getopts ':vn:h' opt; do
  case $opt in
    v) verbose=1 ;;
    n) name=$OPTARG ;;
    h) echo "usage: $0 [-v] [-n name] [files...]"; exit 0 ;;
    :) echo "option -$OPTARG needs a value" >&2; exit 2 ;;
    \?) echo "unknown option: -$OPTARG" >&2; exit 2 ;;
  esac
done
shift $((OPTIND - 1))        # what's left in $@ are the non-option arguments
 
(( verbose )) && echo "verbose mode on"
echo "Hello, $name. Remaining: $*"

Run it as ./args.sh -v -n Robin file1 file2.

06-01 Pipe status

In a pipeline, $? only reports the last command, so earlier failures are hidden.

false | true
echo $?                       # 0, even though false failed
 
false | true
echo "${PIPESTATUS[@]}"       # 1 0  (status of EVERY command in the last pipeline)

PIPESTATUS must be read immediately after the pipeline. To make the whole pipeline fail when any part fails:

set -o pipefail
false | true
echo $?                       # 1

06-02 Timing commands

time sleep 1                  # prints real, user and sys time
 
start=$SECONDS                # $SECONDS counts seconds since the shell started
sleep 2
echo "took $((SECONDS - start))s"
 
start=$(date +%s)             # epoch seconds, works everywhere
long_task
echo "took $(( $(date +%s) - start ))s"
 
echo "$EPOCHREALTIME"         # Bash 5: seconds with microseconds

time is a Bash keyword that can time a whole pipeline, and you can change its output format with TIMEFORMAT:

TIMEFORMAT='took %R seconds'
time sleep 1

07 Code structure

07-00 Sourcing code

source file (or . file) runs another file inside your current shell, so its functions and variables become available. That is how you build libraries.

# lib.sh
greet() {
  printf 'Hello, %s!\n' "$1"
}
#!/usr/bin/env bash
# main.sh
 
dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)   # the folder this script lives in
source "$dir/lib.sh"
 
greet Robin

${BASH_SOURCE[0]} is the path of the current file, which stays correct even when the script is itself sourced. Using it, instead of $0 or the current directory, means your script finds its helper files no matter where you run it from.

A common pattern lets one file work both as a library and as a program:

main() {
  echo "running as a program"
}
 
if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
  main "$@"
fi

07-01 Curlies vs. Parens

The same brackets mean very different things depending on the type:

SyntaxWhat it is
( cmds )Run commands in a subshell (a child copy of the shell)
{ cmds; }Group commands in the current shell
$( cmd )Command substitution: capture output
$(( expr ))Arithmetic: evaluate a number
${ var }Parameter expansion (written without the inner spaces)

The difference between the first two matters:

x=1
( x=2; cd /tmp )              # subshell: changes vanish afterwards
echo "$x $PWD"                # 1 and the original folder
 
{ x=3; cd /tmp; }             # current shell: changes stay
echo "$x $PWD"                # 3 and /tmp

Note the syntax rules for curlies: a space after { and a semicolon (or newline) before }. A subshell is handy for a temporary cd:

( cd build && make )          # you are still in your original folder afterwards

07-02 Return vs. Output

Functions have two separate ways to communicate:

  1. Return status (return 0 to 255) says whether it worked. Use it with if.
  2. Output (what it prints) is how you return data. Capture it with $(...).
is_even() {
  (( $1 % 2 == 0 ))           # the status of this test IS the return value
}
 
if is_even 4; then
  echo "4 is even"
fi
 
add() {
  echo $(( $1 + $2 ))         # "return" a value by printing it
}
 
sum=$(add 2 3)
echo "$sum"                   # 5

Capturing output runs the function in a subshell. If you want to set a variable without that cost, use a nameref (Bash 4.3+) or printf -v:

add_into() {
  local -n out=$1             # "out" is now another name for the variable we were given
  out=$(( $2 + $3 ))
}
 
add_into result 2 3
echo "$result"                # 5
 
printf -v padded '%05d' 42    # write printf's output straight into a variable
echo "$padded"                # 00042

07-03 Chapter 7 recap

A script that is also a library, uses return for success and output for data:

#!/usr/bin/env bash
# mathlib.sh
 
is_positive() { (( $1 > 0 )); }
 
square() { echo $(( $1 * $1 )); }
 
main() {
  for n in "$@"; do
    if is_positive "$n"; then
      echo "$n squared is $(square "$n")"
    else
      echo "$n is not positive" >&2
    fi
  done
}
 
[[ ${BASH_SOURCE[0]} == "$0" ]] && main "$@"

Run ./mathlib.sh 3 -4 5, or source mathlib.sh and call square 9 from your shell.

08 Expansions

08-00 Parameter expansion

${...} can do far more than insert a variable. It replaces a lot of calls to sed, cut and basename.

# Defaults
echo "${name:-guest}"          # use "guest" if name is unset or empty
echo "${name:=guest}"          # same, but ALSO assign it
echo "${name:?name is required}"   # abort with an error if unset or empty
echo "${name:+set}"            # print "set" only if name HAS a value
 
# Length and substrings
s="hello world"
echo "${#s}"                   # 11
echo "${s:6}"                  # world
echo "${s:0:5}"                # hello
 
# Trim from the front (#) or the back (%). Double = longest match.
path=/home/robin/archive.tar.gz
echo "${path##*/}"             # archive.tar.gz   (like basename)
echo "${path%/*}"              # /home/robin      (like dirname)
echo "${path%%.*}"             # /home/robin/archive
echo "${path#*.}"              # tar.gz
echo "${path%.gz}"             # /home/robin/archive.tar
 
# Replace
f="my photo.jpg"
echo "${f/photo/pic}"          # my pic.jpg      (first match)
echo "${f// /_}"               # my_photo.jpg    (all matches)
 
# Case (Bash 4+)
echo "${s^^}"                  # HELLO WORLD
echo "${s^}"                   # Hello world
echo "${s,,}"                  # hello world

Memory trick: # is on the left side of the keyboard's $ (front trimming) and % is on the right (back trimming).

08-01 Array expansion

Many of the same tricks work on whole arrays:

files=(a.txt b.txt c.txt d.txt)
 
echo "${files[@]}"             # every element
echo "${#files[@]}"            # count: 4
echo "${!files[@]}"            # the indexes: 0 1 2 3
echo "${files[@]:1:2}"         # slice: b.txt c.txt
echo "${files[@]%.txt}"        # trim a suffix from EVERY element: a b c d
echo "${files[@]/.txt/.md}"    # substitute in every element
echo "${files[-1]}"            # last element (Bash 4.3+)
 
printf '%s\n' "${files[@]}"    # one element per line
copy=("${files[@]}")           # copy an array
all=("${files[@]}" e.txt)      # combine / append

Always use the quoted "${array[@]}" form to keep elements intact.

09 Globbing

09-00 Basic globbing

A glob is a pattern that Bash itself expands into matching file names before the command runs.

ls *.txt             # any file ending in .txt
ls file?.txt         # ? matches exactly one character
ls file[123].txt     # one of the characters in the brackets
ls file[0-9].txt     # a range
ls [[:upper:]]*      # character classes: [:digit:], [:alpha:], [:upper:], ...
echo *               # a quick look at the files in this folder

Two details that surprise people:

  • * does not match files starting with a dot, and it does not cross /.
  • If nothing matches, Bash leaves the pattern as plain text. A command like ls *.xyz then complains about a file literally named *.xyz.

Because the shell does the expansion, rm * really becomes rm file1 file2 .... Do not confuse globs with regexes: in a glob, * already means "anything".

09-01 Extended globbing

Turn on extra patterns with shopt -s extglob:

shopt -s extglob
 
ls !(*.txt)              # everything EXCEPT .txt files
ls *.@(jpg|png|gif)      # exactly one of the alternatives
ls +([0-9]).log          # one or more digits, then .log
ls ?(pre-)name           # optional prefix: name or pre-name
ls *(ab)                 # zero or more repetitions of "ab"
PatternMatches
?(a)Zero or one occurrence
*(a)Zero or more occurrences
+(a)One or more occurrences
@(a)Exactly one occurrence
!(a)Anything except the pattern

Extended globs are also available in case patterns and in [[ $x == pattern ]].

09-02 Glob shell options

Change how globs behave with shopt:

shopt -s nullglob      # a pattern with no matches becomes EMPTY (not literal text)
shopt -s dotglob       # * also matches hidden files
shopt -s globstar      # ** matches folders recursively (Bash 4+)
shopt -s nocaseglob    # case-insensitive matching
shopt -s failglob      # a pattern with no matches is an error
 
shopt globstar         # check one option
shopt -u dotglob       # -u turns an option off
shopt -s globstar nullglob
for file in **/*.md; do        # every .md file in this folder and below
  echo "$file"
done

nullglob makes loops safe: with it, for f in *.txt runs zero times when there are no .txt files instead of once with the text *.txt.

10 Brace expansion

10-00 Brace expansion

Braces generate lists of words. Unlike globs, they do not look at the filesystem. They just produce text.

echo {a,b,c}                    # a b c
echo file{1,2,3}.txt            # file1.txt file2.txt file3.txt
mkdir -p project/{src,docs,tests}
cp config.yml{,.bak}            # same as: cp config.yml config.yml.bak
echo pre{fix,view,pare}         # prefix preview prepare

10-01 Braces and globbing

Order matters: Bash does brace expansion first, then variables, then globs.

ls *.{jpg,png}          # becomes: ls *.jpg *.png   (then each glob is expanded)
ls ~/Pictures/{2024,2025}/*.jpg

Braces always produce words, even if no file exists. Globs only produce names that match. Compare echo {a,b}.txt (prints a.txt b.txt regardless) with echo *.txt (prints only existing files).

10-02 Numeric brace expansion

echo {1..5}             # 1 2 3 4 5
echo {5..1}             # 5 4 3 2 1
echo {1..10..2}         # 1 3 5 7 9   (step of 2)
echo {a..e}             # a b c d e
echo {01..05}           # 01 02 03 04 05   (zero-padded)
 
mkdir -p backups/{2025..2026}-{01..12}

One gotcha: braces are expanded before variables, so you cannot put a variable in the range:

n=5
echo {1..$n}            # prints literally: {1..5}

Use seq or a C-style loop instead:

seq 1 "$n"
for ((i = 1; i <= n; i++)); do echo "$i"; done

11 Formatting and matching

11-00 Understanding printf

printf is a more predictable echo. It takes a format string followed by values.

printf 'Hello, %s!\n' "Robin"
printf '%d items\n' 3
printf '%.2f\n' 3.14159              # 3.14
printf '%5d|\n' 42                   #    42|   (right-aligned, width 5)
printf '%-8s|\n' "ab"                # ab      | (left-aligned)
printf '%05d\n' 42                   # 00042
printf '%x %o\n' 255 8               # ff 10     (hex and octal)

Common specifiers:

SpecifierMeaning
%sString
%dInteger
%fFloating point
%xHexadecimal
%qQuote the value safely for reuse in the shell
%%A literal percent sign

The format is reused until every argument is consumed, which makes printf great for lists and tables:

printf '%s\n' apple banana cherry          # one per line
printf '%-10s %5s\n' Name Score Alice 93 Bob 87

echo behaves differently across shells (flags like -n and -e, and escape handling), so use printf whenever the exact output matters.

11-01 Date formatting

Bash 4.2+ can format dates without starting another program:

printf '%(%Y-%m-%d)T\n' -1                 # today; -1 means "now"
printf '%(%Y-%m-%d %H:%M:%S)T\n' -1
printf -v stamp '%(%Y%m%d-%H%M%S)T' -1     # store it in a variable
echo "backup-$stamp.tar.gz"

With the external date command:

date +%F                      # 2026-11-17
date +%H:%M                   # 14:05
date +%s                      # epoch seconds
date -d "yesterday" +%F       # GNU date (Linux)
date -v-1d +%F                # BSD date (macOS): yesterday

Handy format codes: %Y year, %m month, %d day, %H hour (24h), %M minute, %S second, %F is %Y-%m-%d, %T is %H:%M:%S.

11-02 Regular expressions

Inside [[ ]], the =~ operator matches a regular expression, and the captured groups land in the BASH_REMATCH array.

email="alice@example.com"
re='^([^@]+)@(.+)$'                     # keep the regex in a variable, UNQUOTED when used
 
if [[ $email =~ $re ]]; then
  echo "user:   ${BASH_REMATCH[1]}"     # alice
  echo "domain: ${BASH_REMATCH[2]}"     # example.com
else
  echo "not an email"
fi

Tips:

  • Store the regex in a variable (single-quoted) and use it without quotes on the right of =~. Quoting it turns it into a literal string match.
  • These are POSIX extended regexes, so \d is not available. Use [0-9] or [[:digit:]].
  • BASH_REMATCH[0] is the whole match, and [1], [2], ... are the groups.

For quick checks on the command line, grep -E uses the same flavour.

11-03 Using mapfile

mapfile (also called readarray) reads lines straight into an array. Bash 4+.

mapfile -t lines < names.txt          # -t strips the trailing newline from each line
echo "read ${#lines[@]} lines"
echo "first: ${lines[0]}"
 
for line in "${lines[@]}"; do
  echo "> $line"
done
 
# From a command
mapfile -t files < <(find . -name "*.sh")

It replaces the classic and clumsy while read loop whenever you want all the lines in memory, and it avoids the subshell problem from 04-06.

12 Tests

12-00 Brackets vs. test

There are three ways to test things:

FormWhat it is
test expr and [ expr ]The classic POSIX test ([ is literally a command)
[[ expr ]]Bash's keyword: safer and more powerful
(( expr ))Arithmetic: use it for numbers

Why prefer [[ ]] in Bash scripts:

file="my file.txt"
 
[ -f $file ]            # BROKEN: word splitting turns this into -f my file.txt
[[ -f $file ]]          # fine: no word splitting or globbing inside [[ ]]
 
[[ $a == abc* ]]        # pattern matching on the right side
[[ $s =~ ^[0-9]+$ ]]    # regex matching
[[ -f $f && -r $f ]]    # && and || work inside
[ -f "$f" -a -r "$f" ]  # the old [ ] needs -a and -o instead

Numbers versus strings:

[[ 10 -gt 9 ]]          # numeric: true
[[ 10 > 9 ]]            # string comparison!: "10" sorts BEFORE "9", so false
(( 10 > 9 ))            # arithmetic: true. Use this for numbers.

The downside: [[ ]] is not POSIX. If your script must run under plain sh (for example #!/bin/sh on Alpine or Debian's dash), stick to [ ] and quote everything.

12-01 Special strings

Normal quotes cannot express special characters like a newline or tab. ANSI-C quoting, $'...', can.

echo $'line one\nline two'
echo $'name\tage'                 # tab
echo $'It\'s fine'                # a quote inside
echo $'\x41'                      # A (a hex byte)
echo $'\e[31mred\e[0m'            # the escape character (used for colours)
 
IFS=$'\n'                         # a newline as the separator
nl=$'\n'
text="first${nl}second"

Compare the quote types:

QuotesVariables expanded?Escapes like \n interpreted?
'...'NoNo
"..."YesNo (only a few like \" and \$)
$'...'NoYes

For longer multi-line strings, use a here-doc (see 03-05).

13 Signals and pipes

13-00 Trap signals

trap lets your script run code when something happens: you press Ctrl-C, the script exits, or a command fails. The classic use is cleanup.

#!/usr/bin/env bash
 
tmp=$(mktemp -d)                  # a private temporary folder
 
cleanup() {
  echo "cleaning up $tmp"
  rm -rf "$tmp"
}
trap cleanup EXIT                 # runs on ANY exit: normal, error or Ctrl-C
 
echo "working in $tmp"
sleep 30

Some useful traps:

trap 'echo "interrupted"; exit 130' INT     # Ctrl-C
trap 'echo "got TERM"; exit 143' TERM       # kill <pid>
trap 'echo "failed at line $LINENO"' ERR    # any command exits non-zero
trap '' INT                                 # ignore Ctrl-C
trap - INT                                  # restore the default behaviour
trap -p                                     # list the current traps
kill -l                                     # list all signal names

You cannot trap SIGKILL (kill -9) or SIGSTOP. These always win, which is why you should try kill (TERM) before kill -9.

13-01 Named pipes

A named pipe (FIFO) is a pipe that has a name in the filesystem, so two unrelated programs can talk through it.

mkfifo /tmp/demo.fifo
ls -l /tmp/demo.fifo            # the type shows as "p"

Open two terminals. In the first one, read:

cat /tmp/demo.fifo              # blocks, waiting for a writer

In the second one, write:

echo "hello through the pipe" > /tmp/demo.fifo

The text appears in the first terminal. Opening a FIFO blocks until the other end is opened too, so a lone echo > fifo just sits there waiting.

A tiny logger that way:

#!/usr/bin/env bash
fifo=$(mktemp -u)
mkfifo "$fifo"
trap 'rm -f "$fifo"' EXIT
 
while true; do
  while read -r line; do
    printf '[%(%T)T] %s\n' -1 "$line"
  done < "$fifo"
done

Now anything written with echo "event" > "$fifo" is logged with a timestamp.

14 Terminal output

14-00 Color output

Terminals understand ANSI escape codes: the escape character followed by [, a code and m.

printf '\e[31mred\e[0m\n'
printf '\e[1;32mbold green\e[0m\n'
printf '\e[38;5;208morange (256 colours)\e[0m\n'
CodeEffect
0Reset everything
1Bold
4Underline
30 to 37Foreground: black, red, green, yellow, blue, magenta, cyan, white
40 to 47The same colours as background
38;5;NForeground colour N from the 256-colour palette

Put them in variables so your script stays readable:

red=$'\e[31m'; green=$'\e[32m'; bold=$'\e[1m'; reset=$'\e[0m'
 
ok()   { printf '%s✔%s %s\n' "$green" "$reset" "$*"; }
fail() { printf '%s✘%s %s\n' "$red" "$reset" "$*" >&2; }
 
ok "build finished"
fail "tests failed"

The tput command looks up the right codes for your terminal:

tput setaf 2; echo "green"; tput sgr0       # sgr0 resets

14-01 Cursor commands

More escape codes move the cursor and erase text, which is how progress bars and spinners work.

CodeEffect
\rCarriage return: back to the start of the line
\e[KErase from the cursor to the end of the line
\e[2JClear the screen
\e[HMove the cursor to the top left
\e[AMove the cursor up one line
# A spinner that rewrites the same line
spin='-\|/'
for i in {1..20}; do
  printf '\r%s working...' "${spin:i%4:1}"
  sleep 0.1
done
printf '\r\e[K done!\n'
 
# A simple progress bar
total=20
for ((i = 0; i <= total; i++)); do
  printf '\r[%-20s] %3d%%' "$(printf '#%.0s' $(seq 1 "$i"))" $((i * 100 / total))
  sleep 0.05
done
echo
 
tput civis        # hide the cursor
sleep 1
tput cnorm        # show it again

If you hide the cursor, restore it with a trap (see 13-00), or an interrupted script leaves your terminal without a cursor.

14-02 Is a TTY

Should your script print colours? Only if a human is looking at a terminal. If the output is piped to a file or another program, escape codes become garbage.

[[ -t 1 ]] && echo "stdout is a terminal"
[[ -t 0 ]] && echo "stdin is a terminal"
[[ -t 2 ]] && echo "stderr is a terminal"

A common pattern:

if [[ -t 1 && -z ${NO_COLOR:-} ]]; then
  red=$'\e[31m'; reset=$'\e[0m'
else
  red=''; reset=''
fi
 
echo "${red}something went wrong${reset}"

Test it: run ./script.sh (colours) and ./script.sh | cat (no colours). Respecting the NO_COLOR environment variable is a nice courtesy for users who dislike colour.

15 Your shell

15-00 PS1 variable

PS1 is the string Bash prints as your prompt. It supports backslash escapes:

EscapeMeaning
\uUsername
\hHostname
\wCurrent directory (with ~ for home)
\WOnly the last part of the directory
\tTime (24h)
\$# for root, $ for everyone else
\nNewline
PS1='\u@\h:\w\$ '

Add colour, and wrap every colour code in \[ and \] so Bash knows those characters take up no width. Without them, long lines wrap badly.

PS1='\[\e[32m\]\u@\h\[\e[0m\]:\[\e[34m\]\w\[\e[0m\]\$ '

The prompt is re-evaluated every time, so you can embed commands. Use single quotes so they are not run too early:

git_branch() { git branch --show-current 2>/dev/null; }
PS1='\w $(git_branch)\$ '

15-01 Customizing Bash

Settings go in your startup files:

  • ~/.bashrc is read by interactive non-login shells (most Linux terminals).
  • ~/.bash_profile is read by login shells (macOS Terminal opens one for every window).

A common fix on macOS is to make the profile load the rc file. Put this in ~/.bash_profile:

[[ -f ~/.bashrc ]] && source ~/.bashrc

Ideas for your ~/.bashrc:

# Aliases
alias ll='ls -lah'
alias ..='cd ..'
alias gs='git status'
 
# History
HISTSIZE=10000
HISTFILESIZE=20000
HISTCONTROL=ignoreboth         # skip duplicates and commands starting with a space
shopt -s histappend            # append to the history file instead of overwriting it
PROMPT_COMMAND='history -a'    # save history after every command
 
# Nicer behaviour
shopt -s autocd                # type a folder name to cd into it
shopt -s cdspell               # fix small typos in cd
export EDITOR=vim
 
# Reload after editing
alias reload='source ~/.bashrc'

15-02 Readline shortcuts

Bash's line editor is called readline. Learning a few shortcuts makes you much faster:

ShortcutAction
Ctrl-a / Ctrl-eJump to the start / end of the line
Alt-b / Alt-fMove back / forward one word
Ctrl-uDelete from the cursor to the start
Ctrl-kDelete from the cursor to the end
Ctrl-wDelete the previous word
Ctrl-yPaste ("yank") what you just deleted
Ctrl-rSearch your history as you type
Ctrl-lClear the screen
Ctrl-cCancel the current command
Ctrl-dEnd of input (exits the shell on an empty line)

On macOS Terminal you may need to enable "Use Option as Meta key" for the Alt shortcuts.

History shortcuts:

!!             # repeat the previous command
sudo !!        # ...with sudo in front
!$             # the last argument of the previous command
history | tail # recent history

Customize readline in ~/.inputrc:

set completion-ignore-case on
set show-all-if-ambiguous on
"\e[A": history-search-backward
"\e[B": history-search-forward

16 Pitfalls

16-00 Pitfall: ls

ls is made for humans, not for scripts. Never parse its output. File names can contain spaces, tabs, even newlines, and ls output cannot be split reliably.

# BAD: breaks on spaces in names
for f in $(ls *.txt); do
  echo "$f"
done
 
# GOOD: let the shell expand the glob, and quote the variable
for f in *.txt; do
  [[ -e $f ]] || continue
  echo "$f"
done
 
# GOOD for recursive searches: NUL-separated names are always safe
find . -type f -name '*.txt' -print0 |
  while IFS= read -r -d '' f; do
    echo "$f"
  done

The same warning applies to for f in $(cat file) and for f in $(find ...).

16-01 Aliases with arguments

An alias is plain text replacement. It cannot take arguments in the middle.

alias ll='ls -lah'
ll /tmp                 # works: expands to ls -lah /tmp (arguments go on the end)
 
alias mkcd='mkdir -p "$1" && cd "$1"'    # DOES NOT WORK: $1 is not an argument here

When you need arguments, or more than one command, write a function:

mkcd() {
  mkdir -p -- "$1" && cd -- "$1"
}
 
mkcd projects/new-idea

Other alias limits: they are not expanded in non-interactive scripts by default, and they only apply to the first word of a command. Functions work everywhere.

16-02 Pitfall: String length

${#var} gives the length, but the question is: length in what?

s='héllo'
echo "${#s}"                    # 5 in a UTF-8 locale (characters)
LC_ALL=C bash -c 's="héllo"; echo "${#s}"'   # 6 in the C locale (bytes)
 
printf '%s' "$s" | wc -m        # characters
printf '%s' "$s" | wc -c        # bytes

So the answer depends on your locale (LANG / LC_ALL). Even in UTF-8, ${#s} counts Unicode code points, not what you see on screen. An emoji with a skin-tone modifier or an accented letter built from combining marks can count as 2 or more.

Why it matters:

  • Truncating or padding text to a width can cut a multibyte character in half.
  • printf '%-10s' pads by bytes in some environments, which misaligns tables that contain non-ASCII text.
  • Validating "max 20 characters" differs from "max 20 bytes". Decide which you mean.

17 Forkbomb

A fork bomb is a program that keeps creating copies of itself. Each copy creates more copies, the number of processes grows exponentially, and the machine runs out of process slots and memory until it stops responding. It is a classic example of a denial-of-service attack, and sometimes just an unfortunate typo.

I am deliberately not printing a working one here. The point of the chapter is to understand the idea and, more importantly, how to protect yourself. The course video shows it in a safe context, so watch that section (17-00) if you want to see it in action.

How to defend a system:

ulimit -u                       # the maximum number of processes for your user
ulimit -u 2048                  # lower it for the current shell (can only go down)

For permanent limits on Linux:

# /etc/security/limits.conf: limit user "robin" to 4096 processes
robin  hard  nproc  4096

On systemd-based systems, you can cap the number of tasks per service or per user:

# in a unit file (or a drop-in)
[Service]
TasksMax=512

Containers help too: Docker has --pids-limit, and cgroups expose pids.max. If a machine is already melting down, a new shell may not be able to start, because there are no free process slots, so the best fix is to set the limits beforehand.

Never run code you do not understand, especially a one-liner full of punctuation that you found online. If you cannot explain what each part does, do not paste it into a terminal.

Bonus: Debugging session

Scripts fail in confusing ways, so learn these tools early.

bash -n script.sh            # check the syntax without running anything
bash -x script.sh            # run while printing every command, with values expanded

Turn tracing on and off for just a part of a script:

set -x                       # start tracing
risky_function
set +x                       # stop tracing

Make the trace useful by customizing PS4, the prefix of each traced line:

export PS4='+ ${BASH_SOURCE[0]}:${LINENO}: ${FUNCNAME[0]:-main}: '
bash -x script.sh

Safer defaults you will see in many scripts:

set -u                       # error on unset variables (catches typos)
set -o pipefail              # a pipeline fails if any command in it fails
set -e                       # exit when a command fails

Use set -e with caution. It has surprising exceptions: it is ignored inside if conditions, in && and || lists and in pipelines (without pipefail). Many people prefer explicit checks (cmd || exit 1) plus a trap ... ERR.

Finally, install ShellCheck. It reads your script and points out quoting mistakes, bad tests and portability problems, usually with a link that explains why:

shellcheck script.sh

Cheat sheet: habits that prevent bugs

HabitWhy
Quote variables: "$var", "$@", "${arr[@]}"Stops word splitting and glob expansion
No spaces around = in assignmentsx = 1 runs a command called x
Use [[ ]] in Bash, (( )) for numbersFewer surprises than [ ]
Use local in functionsAvoids leaking variables
Use printf instead of echoPredictable output
Send errors to stderr: echo "..." >&2Keeps data and messages separate
Check command -v tool before using itClear error instead of a crash
Use mktemp plus trap ... EXITTemporary files get cleaned up
Use "${var:?}" before rm -rfPrevents deleting the wrong thing
Use globs or find -print0, not lsWorks with any file name
Run ShellCheckCatches most mistakes for free

Where to go next

  1. Watch the course. Seeing the commands typed live, with the explanations, is hard to beat. Use this guide as a reference alongside it.
  2. Read the notes and code. The course repository on GitHub has Dave's own notes and code for each section, plus the same foo directory he builds during the videos. It is the best place to compare my examples with the originals.
  3. Write small scripts for real chores. A backup script, a script that renames photos, a tiny deploy helper. Every script you write makes the next one easier.
  4. Keep the man pages open. Most answers are one man or help away.

Credits

This post is a study companion. The course itself, including the structure that I followed here, was created by Dave Eddy. If this guide helped you, please go and support the original.

I am not affiliated with Dave Eddy or You Suck at Programming. The explanations and code examples in this post are my own, and any mistakes are mine too. If you find one, please let me know.

Share this post