Shell Scripting Basics — DevOps

A deploy script (a file of commands that installs a new version of the app) starts with the shebang #!/bin/bash (or #!/usr/bin/env bash), the line that picks…

At eleven at night you deploy notes-api. The script finishes without a complaint, and you go to bed. Next morning people using the app still find the bug that was supposedly fixed: the server is still running yesterday's code, and the script gave no warning.

A shell script is a file of commands that bash (the most common shell on Linux) runs line by line, exactly as if you typed them one by one. On a server, the most important script is the deploy script, which installs a new version of the app. Its steps: fetch the new code, install the dependencies (the npm packages the app uses), run the database migrations (changes to the tables), then restart the app. Picture a pilot's pre-flight checklist: if one check fails, the plane does not take off. Each deploy step is one check on that checklist. A good deploy script stops as soon as one step fails, and says that the deploy failed. The basics (shebang, variables, if, $?) are in the Shell Script Basics lesson of the Terminal track.

deploy.sh for notes-api

This is the deploy script of notes-api:

#!/bin/bash
set -euo pipefail

APP_DIR="/srv/notes-api"
BRANCH="${1:-main}"

echo "Deploying notes-api from branch $BRANCH"
cd "$APP_DIR"
git pull --ff-only origin "$BRANCH"
npm ci --omit=dev
node migrate.js | tee -a deploy.log
sudo systemctl restart notes-api
echo "Deploy complete!"

Once only: chmod u+x deploy.sh (the File System & Permissions lesson), then run it with ./deploy.sh.

Exit codes: 0 is success, anything else is failure

Every command finishes with an exit code: 0 means success, any other number means failure. git uses 128 for a fatal error (an error that makes git stop). echo $? prints the last command's exit code. This is the same deploy.sh, without the set line, when GitHub cannot be reached. Before you read the output, guess: what is the last line printed, and what does echo $? print?

deploy@web-1:/srv/notes-api$ ./deploy.sh
Deploying notes-api from branch main
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

added 64 packages, and audited 65 packages in 3s

found 0 vulnerabilities
No new migrations.
Deploy complete!
deploy@web-1:/srv/notes-api$ echo $?
0

git pull failed with exit 128, but bash still moved on to the next line. npm ci reinstalled the old code's dependencies, and the app was restarted on the old code. Then the script printed Deploy complete! and exited with 0, because a script's exit code is the exit code of its last command, the echo. That is what happened that night, and CI (a server that runs the deploy automatically) or a teammate reading the exit code would think the deploy worked.

set -euo pipefail: three safeguards

With set -euo pipefail on the second line, the same event stops right there:

deploy@web-1:/srv/notes-api$ ./deploy.sh
Deploying notes-api from branch main
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
deploy@web-1:/srv/notes-api$ echo $?
128

This table shows what each safeguard catches. The line numbers are those of deploy.sh above:

Safeguard What it catches in deploy.sh Without it
-e git pull fails: the script stops at line 9, exit 128 it goes on to Deploy complete!
-u the typo $BRANH: ./deploy.sh: line 7: BRANH: unbound variable, exit 1 $BRANH silently becomes empty text
-o pipefail node migrate.js fails inside a pipe: it stops at line 11, exit 1 the pipe counts as a success because tee succeeded

Quote every variable

Without quotes, bash splits a variable's value at spaces before it runs the command. In this example, touch makes an empty file:

LOG="storage/logs/deploy 2026-10-09.log"
touch $LOG     # two files: "storage/logs/deploy" and "2026-10-09.log"
touch "$LOG"   # one file, with exactly that name

Make it a habit to write "$APP_DIR", "$BRANCH", "$file". Double quotes still replace a variable with its value, single quotes ('$APP_DIR') do not: you get the text $APP_DIR as it is.

Loops: wait until the app is ready

After a restart, the app needs a few seconds before it is ready. The loop below checks it up to five times. for runs its body (the part between do and done) once for each item, here the numbers 1 to 5:

for i in 1 2 3 4 5; do
  if curl -fsS http://127.0.0.1:3000/health > /dev/null; then
    echo "Health check OK"
    exit 0
  fi
  sleep 2
done
echo "Health check failed after 5 tries" >&2
exit 1

A deploy script from an array of lines

In the exercise, generateDeployScript(config) builds the script's text from an array of lines joined with "\n": the shebang #!/bin/bash, set -euo pipefail, one echo with the app name and branch, every step from config.steps (use the spread ...config.steps, which inserts everything in the array), then echo "Deploy complete!".

⚠️ When a deploy script lies, or stops for no reason

TL;DR: A deploy script (a file of commands that installs a new version of the app) starts with the shebang #!/bin/bash (or #!/usr/bin/env bash), the line that picks bash to run it. The next line is set -euo pipefail: -e stops at the first failed command, -u refuses variables that were never set, -o pipefail makes a pipe (|) fail when any of its parts fails. Exit code 0 means success, anything else failure, and echo $? shows it. Without -e, a failed script still exits with 0 from its last echo. Variables are written NAME="value" with no spaces and read as "$NAME" in quotes, and "${1:-main}" gives the first argument the fallback value main. for x in ...; do ...; done repeats for each item. sudo in a script needs a NOPASSWD (no password) rule for that one command.

⚡ The first two lines

Write:

#!/bin/bash
set -euo pipefail

Meaning: The shebang (the first line, starting with #!) tells the system that bash runs this file. set -euo pipefail adds three safeguards, so the script stops as soon as something goes wrong.

⚡ Exit codes: 0 means success

Write:

deploy@web-1:/srv/notes-api$ echo $?
0
Guess first: a deploy script runs git pull (fetch the new code), a few other commands, then echo "Deploy complete!". Without the set line, git pull fails with exit code (the number a command ends with) 128. What is the script's exit code?

0. Bash moves on to the next line, prints Deploy complete!, and a script's exit code is the exit code of its last command, the echo.

Meaning: 0 means success, any other number means failure, and echo $? prints it. With -e, the script stops at the first failed command, here git pull, and exits with its exit code: 128.

⚡ A pipe can swallow the error

Write:

node migrate.js | tee -a deploy.log

Meaning: The pipe (|) sends the database migration's output to tee, which writes it on screen and into deploy.log. A pipe's exit code is the exit code of its last command. Without pipefail, a failed migration still counts as a success because tee succeeded. -u refuses variables that were never set, like $BRANH, a typo of the variable $BRANCH.

⚡ Quote every variable

Write:

LOG="storage/logs/deploy 2026-10-09.log"
touch "$LOG"

Meaning: touch makes an empty file. Without quotes, touch $LOG makes two files, because bash splits the value at the space. With "$LOG", you get one file with exactly that name.

What you will learn