Skip to main content

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.

flowchart TD A[deploy.sh starts] --> B[Expand variables and globs] B --> C[fork and exec command] C --> D{Exit code 0?} D -->|Yes| E[Next command] D -->|No| F{set -e active?} F -->|Yes| G([Script exits]) F -->|No| E

How to Write Safer Bash Scripts

Start every production script with strict mode and quote your variables.

Bash
#!/usr/bin/env bash
set -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'
done

Practical Bash Features

Bash
DATE=$(date +%Y%m%d) # command substitution
FILES=$((TOTAL - DELETED)) # arithmetic
FILENAME="deploy-2026-01-15.tar.gz"
echo "${FILENAME%.tar.gz}" # strip suffix: deploy-2026-01-15
echo "${FILENAME##*.}" # extension: gz
echo "${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-api
fi

Troubleshooting 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.sh prints every command with a + prefix after expansion, so you see the real values your variables hold at runtime.

Security

Never pass user input or external data to eval. It executes arbitrary code. Arrays, parameter expansion, and explicit parsing are almost always safer.

Common Mistake

Leaving variables unquoted. rm -rf $DIR breaks or does damage when $DIR contains spaces or is empty. Write rm -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`.

Found a mistake, an outdated step or a broken link?