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 bashon line one says which interpreter runs the file. set -euo pipefailswitches on safety:-estops at the first failing command,-utreats an unset variable as an error, andpipefailmakes 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.