Skip to the workspace
Pebellslearn by building
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.

devops / Bash Scripting — Commands With Safety Switches

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.