Bash
Bash (Bourne Again Shell) is the default command-line shell on most Linux servers and a scripting language for automation. Deployment scripts, health checks, and CI/CD pipeline steps are commonly written in it.
Understanding Bash
What Is Bash and Why Do DevOps Engineers Use It?
Bash is the prompt you get when you SSH into a server and the interpreter that runs most deployment and CI/CD scripts. It is preinstalled almost everywhere, needs no extra runtime, and composes naturally with tools like grep, awk, curl, and jq. That is why it remains the default glue language for infrastructure work.
How Bash Executes a Script
Bash reads the script line by line, expands variables, globs, and command substitutions, then forks and execs each command. By default a failing command does not stop the script, which is why strict mode matters.
How to Write Safer Bash Scripts
Start every production script with strict mode and quote your variables.
#!/usr/bin/env bashset -euo pipefail# -e exit on any command failure# -u unset variables are errors# -o pipefail a pipeline fails if any stage fails log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"; } cleanup() { rm -f /tmp/deploy.lock; }trap cleanup EXIT SERVERS=("10.0.1.50" "10.0.1.51" "10.0.1.52")for server in "${SERVERS[@]}"; do log "Restarting nginx on $server" ssh "rahul@$server" 'sudo systemctl restart nginx'donePractical Bash Features
DATE=$(date +%Y%m%d) # command substitutionFILES=$((TOTAL - DELETED)) # arithmeticFILENAME="deploy-2026-01-15.tar.gz"echo "${FILENAME%.tar.gz}" # strip suffix: deploy-2026-01-15echo "${FILENAME##*.}" # extension: gzecho "${APP_PORT:-4000}" # default if unset if systemctl is-active --quiet payment-api; then log "Service is running"else log "Service is down, starting" systemctl start payment-apifiTroubleshooting Bash
| Symptom | Command | What to Check |
|---|---|---|
| Script continues after error | Add set -e |
Error handling not enabled |
| Unbound variable | Add set -u |
Variable used before set |
| Syntax error | bash -n script.sh |
Parse without running |
| Logic error | bash -x script.sh |
Trace each command |
Tip
bash -x script.shprints every command with a+prefix after expansion, so you see the real values your variables hold at runtime.
SecurityNever pass user input or external data to
eval. It executes arbitrary code. Arrays, parameter expansion, and explicit parsing are almost always safer.
Common MistakeLeaving variables unquoted.
rm -rf $DIRbreaks or does damage when$DIRcontains spaces or is empty. Writerm -rf "$DIR".
Frequently Asked Questions
Why is Bash still the default for CI/CD and deployment scripts?
It is preinstalled on nearly every Linux server and CI runner, so a Bash script has no runtime to install, and it composes directly with the Unix toolchain (pipes, grep, awk, curl). That makes it the lowest-friction choice for short automation tasks.
What does set -euo pipefail actually do?
`-e` exits when a command fails, `-u` treats unset variables as errors, and `-o pipefail` makes a pipeline fail if any stage fails instead of only the last. Together they stop a half-failed deployment from silently continuing.
What is the difference between bash and sh?
`sh` is the POSIX shell interface and on some distributions (Debian, Ubuntu) points to the smaller dash shell. Bash adds arrays, `[[ ]]` tests, and process substitution, so a script using those features must use `#!/usr/bin/env bash`, not `#!/bin/sh`.