Ravindra BagaleCourses & study guides

Chapter 5: Bash Scripting for Automation

5.3 Variables, quotes and exit codes

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

APP_URL="http://localhost:8080/health"
NAME="badge API"

echo "Checking ${NAME} at ${APP_URL}"

Quotes. Prefer double quotes around expansions: "${APP_URL}". That keeps spaces safe. Single quotes are literal — no expansion.

Exit codes. Every command returns a number. 0 means success. Non-zero means failure. Scripts and CI use this.

curl -fsS "$APP_URL" >/dev/null
echo "curl exit was $?"
Code Usual meaning
0 Success
Non-zero Something failed — read the command docs

In conditions you will write if curl …; then — Bash treats exit 0 as true for if.

Practice task

Write three lines: assign PORT=8080, echo it inside a sentence with double quotes, then explain what $? shows after a successful true command.