Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Shell Scripting Command Guide for Bash, Sh & Zsh

Turning commands into programs.

What Is Shell Scripting, and Why Use It?

A shell script is a text file full of commands — the same ones you type at the prompt — saved with a .sh extension and made executable. When you run it, the shell executes the lines top to bottom. Variables, conditionals, and loops turn those lines from a recording of your typing into an actual program.

Why Learn Shell Scripting?

  • Anything you type more than twice belongs in a script. Automation is the whole point of the command line.
  • Scripts are the standard glue for servers, cron jobs, backups, and deploy steps.
  • The syntax looks intimidating; in practice you need five constructs — variables, conditionals, loops, functions, exit codes — to do almost everything.

A shell script is just a list of commands with superpowers: variables, decisions, and repetition. If you can type it, you can script it.

1. The Shebang Line

The first line tells the system which interpreter to use:

#!/bin/bash          # run with bash
#!/bin/sh            # run with the POSIX shell
#!/usr/bin/env python3   # the same trick works for any language
chmod +x deploy.sh   # make it executable
./deploy.sh          # run it

2. Variables

name="Ada"           # no spaces around =
count=3
echo "$name has $count files"
echo "${name}!"      # braces mark the variable's end
unset name           # delete a variable
readonly PI=3.14159  # make it unchangeable

3. Conditionals

if [ -f "/etc/passwd" ]; then
    echo "file exists"
elif [ -d "/tmp" ]; then
    echo "tmp is a directory"
else
    echo "nothing found"
fi
# Common test operators
[ "$a" = "$b" ]      # string equality
[ "$a" -eq 3 ]       # numeric equality (-ne, -lt, -gt, -le, -ge)
[ -f file ]          # is a regular file?
[ -d dir ]           # is a directory?
[ -x cmd ]           # is it executable?
[ -z "$s" ]          # is the string empty?

4. Loops

for file in *.log; do
    wc -l "$file"
done
i=1
while [ $i -le 5 ]; do
    echo "line $i"
    i=$((i + 1))
done

5. Functions and Exit Codes

greet() {
    echo "Hello, $1"   # $1 is the first argument
}
greet "Ada"
ls /does/not/exist
echo $?               # exit code: 0 = success, anything else = failure

Common Pitfalls

MistakeFix
name = "Ada" failsNo spaces around =
Script runs but command not foundCheck the shebang, or run with bash script.sh
[ -f $file ] breaks on empty valuesAlways quote: [ -f "$file" ]
Missing fi / doneEvery if needs fi; every for/while needs done
Forgetting chmod +xchmod +x script.sh before ./script.sh
set -e surprisesIt exits on any failed command — great for scripts, confusing at first
PageWhat It Covers
AliasesQuick shortcuts; functions are the scriptable version
Environment VariablesPassing data into scripts via the environment
$PATHWhy scripts need absolute paths or a correct PATH
PipesComposing commands inside your scripts
RedirectionCapturing script output to files
bash / sh / zshWhich interpreter your shebang should name

Shell Scripting Cheatsheet

TaskSyntax
Shebang#!/bin/bash
Define a variablename="value"
Read a variableecho "$name"
Delete a variableunset name
if/elseif [ ... ]; then ... else ... fi
File test[ -f file ], [ -d dir ], [ -x cmd ]
for loopfor x in ...; do ... done
while loopwhile [ ... ]; do ... done
Functionname() { ... }
Exit codeecho $?
Make executablechmod +x script.sh
Run it./script.sh