Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Terminal Knowledge  ›  Lesson

Bash Scripting — Commands With Safety Switches

Bash 12 minA Bash script saves a sequence of commands, and a shebang, set -euo pipefail and quoted variables make it stop safely at the first failure.

Bash Scripting — Commands With Safety Switches

Deploying notes-api takes five commands: pull the code, install dependencies, run the tests, restart the service, check it answers. Typed by hand, one typo or one skipped failure ships a broken release. Saved as a Bash script, it runs the same way every time and stops the moment something goes wrong.

The rule

A script is your typed commands, saved, with the safety switches on.

A Bash script is a text file of the same commands you would type. Three things turn it from a list into a safe tool:

#!/usr/bin/env bash
set -euo pipefail

APP_DIR="/srv/notes-api"
cd "$APP_DIR"
git pull --ff-only
./.venv/bin/pip install -r requirements.txt
./.venv/bin/pytest -q
sudo systemctl restart notes-api
curl --fail --silent https://notes.example.com/health
echo "deploy finished"
  • The shebang #!/usr/bin/env bash on line one says which interpreter runs the file.
  • set -euo pipefail switches on safety: -e stops at the first failing command, -u treats an unset variable as an error, and pipefail makes a pipeline fail if any stage fails, not only the last one.
  • Quoted variables. "$APP_DIR" keeps a path with spaces as one argument.

Every command returns an exit code: 0 means success, anything else means failure. set -e reads these codes for you, and $? holds the last one if you want to check it yourself. Make the file executable once with chmod +x deploy.sh, then run it as ./deploy.sh.

The shortcut

Before you trust a script, run the three-switch check: shebang on line one, set -euo pipefail on line two, and every $VARIABLE wrapped in double quotes. Most beginner script disasters come from missing one of the three.

What happens when the tests failStep 1 of 4
  1. Pull and install

    git pull and pip install succeed, each returning exit code 0.

Without set -e, Bash would ignore the failure, restart the service with broken code and happily print "deploy finished".

Drills

Answer fast — trust the rule.

quick drill

What exit code means a command succeeded?

quick drill

What does set -e do in a Bash script?

quick drill

FILE="my notes.txt", then rm $FILE. What goes wrong?

quick drill

You created deploy.sh, and ./deploy.sh says "Permission denied". What is the fix?

Remember

  • A script is your typed commands, saved, with the safety switches on.
  • Shortcut: shebang, set -euo pipefail, quoted variables.
  • Trap: a script that ignores a failure and carries on as if everything worked.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainMake the deploy script safe

A colleague's deploy script has no shebang, no set line and uses cd $APP_DIR. Explain what could go wrong and how you would fix each problem.