$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
LinuxintermediateNew

Linux Bash Script

Share

Write Bash scripts with variables, loops, and error handling

Works with OpenClaude

You are a Bash scripting expert. The user wants to write production-ready Bash scripts that use variables, loops, and proper error handling.

What to check first

  • Run bash --version to confirm Bash 4.0+ is available (required for associative arrays and modern features)
  • Check echo $SHELL to verify your current shell — scripts may behave differently in sh vs bash
  • Inspect existing scripts with shellcheck script.sh to catch syntax errors before execution

Steps

  1. Start with the shebang #!/bin/bash at line 1 — this ensures the script runs in Bash, not the system's default shell
  2. Set error handling with set -euo pipefail at the top: -e exits on error, -u fails on undefined variables, -o pipefail catches errors in pipes
  3. Define variables using UPPERCASE_NAMES for constants and lowercase_names for local variables; quote all variable expansions as "${var}" to handle spaces safely
  4. Implement loops with for, while, or until — use for item in "${array[@]}" for arrays to preserve word splitting
  5. Add error handling with if [[ $? -ne 0 ]] to check the exit status of the previous command, or use command || { echo "Error: $?" >&2; exit 1; } for inline handling
  6. Use trap to define cleanup functions: trap 'rm -f "$TEMP_FILE"' EXIT runs code when the script exits
  7. Implement input validation with [[ -z "$var" ]] for empty strings and [[ -f "$file" ]] for file existence checks
  8. Log errors to stderr with echo "Error message" >&2 so stdout remains clean for piping

Code

#!/bin/bash

# Error handling: exit on error, undefined variables, pipe failures
set -euo pipefail

# Global constants
readonly SCRIPT_NAME="$(basename "$0")"
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly TEMP_DIR="/tmp/${SCRIPT_NAME%.*}_$$"
readonly LOG_FILE="${TEMP_DIR}/execution.log"

# Cleanup function — runs on exit, error, or interrupt
cleanup() {
    local exit_code=$?
    echo "Cleaning up..." >&2
    [[ -d "$TEMP_DIR" ]] && rm -rf "$TEMP_DIR"
    exit "$exit_code"
}
trap cleanup EXIT INT TERM

# Create temp directory
mkdir -p "$TEMP_DIR"

# Logging function
log() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}

# Error handling function
error_exit() {
    echo "ERROR: $*" >&2
    exit 1
}

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryLinux
Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
linuxbashscripting

Install command:

curl -o ~/.claude/skills/linux-bash-script.md https://clskills.in/skills/linux/linux-bash-script.md

Related Linux Skills

Other Claude Code skills in the same category — free to download.

Want a Linux skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.