πŸ”§ DevOps

Bash

Bourne Again Shell

The default Unix/Linux shell. Automation through scripting. An essential DevOps tool.

Detailed Explanation

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.

Code Example

#!/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!"

How It's Said in Practice

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."

Cautions

Learn More