Bash
Bourne Again Shell
The default Unix/Linux shell. Automation through scripting. An essential DevOps tool.
Bourne Again Shell
The default Unix/Linux shell. Automation through scripting. An essential DevOps tool.
Bash (Bourne Again Shell) is a Unix shell developed by Brian Fox in 1989 for the GNU Project. It began as a free software replacement for the Bourne Shell (sh), and today it has become the most widely used shell, serving as the default shell on Linux and macOS.
It provides programming features such as command execution, pipelines, variables, conditionals, and loops. It can be used as an interactive shell or written as script files for automation tasks. It also offers a rich set of convenience features such as Tab completion, history, and aliases.
In DevOps, Bash is essential for writing build scripts, deployment automation, system administration, and CI/CD pipelines. It is mainly used in Docker container ENTRYPOINTs, Kubernetes initialization scripts, and the run step of GitHub Actions.
In practice, best practices are linting scripts with shellcheck, running safely with set -euo pipefail, and modularizing with functions and variables. Complex logic can be replaced with Python or similar, but Bash is still efficient for quick automation.
#!/bin/bash
# deploy.sh - μμ ν λ°°ν¬ μ€ν¬λ¦½νΈ μμ
set -euo pipefail # μλ¬, λ―Έμ μ λ³μ, νμ΄ν μ€ν¨ μ μ’
λ£
# νκ²½λ³μ κ²μ¦
: "${APP_NAME:?APP_NAME νκ²½λ³μ νμ}"
: "${VERSION:?VERSION νκ²½λ³μ νμ}"
: "${ENV:=staging}" # κΈ°λ³Έκ° μ€μ
# ν¨μ μ μ
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
rollback() {
log "ERROR: λ°°ν¬ μ€ν¨, λ‘€λ°± μμ..."
kubectl rollout undo deployment/"${APP_NAME}" -n "${ENV}"
exit 1
}
trap rollback ERR
# λ°°ν¬ μ€ν
log "Deploying ${APP_NAME}:${VERSION} to ${ENV}"
kubectl set image deployment/"${APP_NAME}" \
app="${REGISTRY}/${APP_NAME}:${VERSION}" -n "${ENV}"
# λ‘€μμ λκΈ°
kubectl rollout status deployment/"${APP_NAME}" -n "${ENV}" --timeout=300s
log "Deployment completed successfully!"
Senior: "When you write CI pipeline scripts, be sure to include set -e. Otherwise, it keeps going even if a command fails midway."
Junior: "I heard pipefail is needed too, right?"
Senior: "Yes, you need it to catch failures of commands in the middle of a pipeline as well. set -euo pipefail is the standard starting pattern."
Interviewer: "Why do you use double quotes when using variables in Bash scripts?"
Candidate: "To safely handle values that contain spaces or special characters. Using \"$var\" instead of $var prevents word splitting. It is especially important when handling file paths or user input."
Take a mock interview on this topic β WIA SOOM AI Interview Β· Start for free
Reviewer: "Here, if you write grep pattern file instead of cat file | grep pattern, you can save one process. It's called UUOC (Useless Use of Cat)."
Developer: "Ah, right. And I ran shellcheck and there are other warnings too, so I'll fix those."