AgentSkillsCN

bash

适用于编写 Shell 脚本,或在 macOS 与 Linux 上使用 Bash、Zsh 或 POSIX 兼容的 Shell。涵盖脚本编写基础、变量、控制流、函数、管道、进程管理,以及用于自动化与开发者工具的常用模式。 适用场景:Bash、Zsh、Shell 脚本编写、POSIX Shell、管道、重定向、进程替换、Shell 函数、Shell 变量、.bashrc、.zshrc、shebang、here 文档、命令替换、Shell 算术。 不适用场景:PowerShell 脚本编写(应使用 PowerShell-Core)、Windows 批处理文件、复杂数据处理超出文本范畴(可考虑使用 Python 或 jq)。

SKILL.md
--- frontmatter
name: bash
description: |
    Use when writing shell scripts or working with Bash, Zsh, or POSIX-compatible shells on macOS and Linux. Covers scripting fundamentals, variables, control flow, functions, pipes, process management, and common patterns for automation and developer tooling.
    USE FOR: Bash, Zsh, shell scripting, POSIX shell, pipes, redirection, process substitution, shell functions, shell variables, .bashrc, .zshrc, shebang, here documents, command substitution, shell arithmetic
    DO NOT USE FOR: PowerShell scripting (use powershell-core), Windows batch files, complex data processing beyond text (consider Python or jq)
license: MIT
metadata:
  displayName: "Bash & Shell Scripting"
  author: "Tyler-R-Kendrick"
compatibility: claude, copilot, cursor

Bash & Shell Scripting

Overview

Bash is the default shell on most Linux distributions and was the macOS default until Catalina (now Zsh). Shell scripting is the glue that connects CLI tools together and automates repetitive tasks. Most CI/CD pipelines, Docker entrypoints, and deployment scripts are Bash. Understanding shell scripting is a foundational skill for every developer working in Unix-like environments.

Bash vs Zsh vs Fish

FeatureBashZshFish
CompatibilityPOSIXMostly POSIXNot POSIX
Default OnMost Linux distrosmacOS (Catalina+)
Plugin EcosystemMinimalOh My Zsh / StarshipBuilt-in
Auto-completeBasicExtensiveExcellent, built-in
ScriptingStandardBash-compatible + extrasUnique syntax

Tip: Write portable scripts in Bash (or POSIX sh) for maximum compatibility. Use Zsh/Fish features interactively but avoid them in shared scripts.


Fundamentals

Shebang

Every script should start with a shebang line that tells the OS which interpreter to use:

bash
#!/usr/bin/env bash    # Portable — finds bash in PATH
#!/bin/bash            # Absolute path — less portable
#!/bin/sh              # POSIX shell — most compatible, fewest features

Variables

bash
# Assignment (no spaces around =)
name="world"
count=42
readonly PI=3.14159    # Constant — cannot be reassigned

# Usage (always quote to handle spaces/special chars)
echo "Hello, ${name}"
echo "Count is: $count"

# Environment vs local
export GLOBAL_VAR="visible to child processes"
local_var="only in this shell"

# Default values
echo "${MISSING_VAR:-default_value}"    # Use default if unset
echo "${MISSING_VAR:=default_value}"    # Set and use default if unset

Special Variables

VariableMeaning
$0Script name
$1-$9Positional arguments
${10}Positional args beyond 9
$#Number of arguments
$@All arguments (as separate words)
$*All arguments (as single string)
$?Exit code of last command
$$PID of current shell
$!PID of last background process
$_Last argument of previous command

Quoting

bash
name="world"

# Single quotes — literal, no expansion
echo 'Hello, $name'          # Output: Hello, $name

# Double quotes — variable expansion, preserves spaces
echo "Hello, $name"          # Output: Hello, world

# Command substitution
today=$(date +%Y-%m-%d)      # Preferred — $() syntax
today=`date +%Y-%m-%d`       # Legacy — backticks (avoid nesting)

# Arithmetic
result=$((5 + 3))
echo "Sum: $result"          # Output: Sum: 8

Control Flow

if / elif / else / fi

bash
# String comparison
if [[ "$name" == "world" ]]; then
    echo "Hello, world!"
elif [[ "$name" == "bash" ]]; then
    echo "Hello, bash!"
else
    echo "Hello, stranger!"
fi

# Numeric comparison
if [[ $count -gt 10 ]]; then
    echo "Count is greater than 10"
fi

# File tests
if [[ -f "$file" ]]; then
    echo "File exists"
elif [[ -d "$dir" ]]; then
    echo "Directory exists"
fi

Common Test Operators

OperatorTypeMeaning
-fFileFile exists
-dFileDirectory exists
-eFilePath exists
-rFileReadable
-wFileWritable
-xFileExecutable
-sFileFile is non-empty
-zStringString is empty
-nStringString is non-empty
==StringStrings are equal
!=StringStrings are not equal
-eqNumericEqual
-neNumericNot equal
-ltNumericLess than
-gtNumericGreater than
-leNumericLess than or equal
-geNumericGreater than or equal

for Loops

bash
# Iterate over a list
for item in apple banana cherry; do
    echo "Fruit: $item"
done

# Iterate over files
for file in *.txt; do
    echo "Processing: $file"
done

# C-style for loop
for ((i = 0; i < 10; i++)); do
    echo "Index: $i"
done

# Iterate over command output
for user in $(cut -d: -f1 /etc/passwd); do
    echo "User: $user"
done

while / until Loops

bash
# while loop
count=0
while [[ $count -lt 5 ]]; do
    echo "Count: $count"
    ((count++))
done

# until loop (runs until condition is true)
until [[ $count -eq 0 ]]; do
    echo "Countdown: $count"
    ((count--))
done

# Read lines from a file
while IFS= read -r line; do
    echo "Line: $line"
done < input.txt

case Statements

bash
case "$1" in
    start)
        echo "Starting service..."
        ;;
    stop)
        echo "Stopping service..."
        ;;
    restart)
        echo "Restarting service..."
        ;;
    status|info)
        echo "Checking status..."
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

Functions

bash
# Function declaration
greet() {
    local name="${1:-World}"    # Local variable with default
    echo "Hello, ${name}!"
}

# Call the function
greet "Bash"    # Output: Hello, Bash!
greet           # Output: Hello, World!

# Return values (0 = success, 1-255 = error)
is_even() {
    local num=$1
    if (( num % 2 == 0 )); then
        return 0    # Success / true
    else
        return 1    # Failure / false
    fi
}

if is_even 4; then
    echo "4 is even"
fi

# Capture output instead of using return for data
get_timestamp() {
    date +%Y%m%d_%H%M%S
}
ts=$(get_timestamp)
echo "Timestamp: $ts"

Pipes and Redirection

bash
# Pipe — send stdout of one command to stdin of another
ls -la | grep ".txt" | sort -k5 -n

# Redirect stdout to file (overwrite)
echo "Hello" > output.txt

# Redirect stdout to file (append)
echo "World" >> output.txt

# Redirect stderr to file
command 2> errors.log

# Redirect both stdout and stderr to file
command > output.log 2>&1
command &> output.log          # Bash shorthand

# Discard output
command > /dev/null 2>&1

# Redirect stdin from file
sort < unsorted.txt

# Here document
cat <<EOF
Hello, $name!
Today is $(date).
EOF

# Here document (no variable expansion)
cat <<'EOF'
This is literal: $name
No expansion here.
EOF

# Here string
grep "pattern" <<< "$variable"

# Process substitution
diff <(sort file1.txt) <(sort file2.txt)

Common Patterns

Script Template

bash
#!/usr/bin/env bash
set -euo pipefail    # Exit on error, undefined vars, pipe failures
IFS=$'\n\t'          # Safer word splitting

# Cleanup trap
cleanup() {
    echo "Cleaning up..."
    rm -f "$tmp_file"
}
trap cleanup EXIT

# Constants
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_NAME="$(basename "$0")"

# Temporary file
tmp_file=$(mktemp)

# Main logic
main() {
    echo "Running ${SCRIPT_NAME} from ${SCRIPT_DIR}"
    # Your code here
}

main "$@"

Parsing Command-Line Arguments

bash
# Using while/case
usage() {
    echo "Usage: $0 [-v] [-o output] [-n count] input_file"
    exit 1
}

verbose=false
output=""
count=1

while [[ $# -gt 0 ]]; do
    case "$1" in
        -v|--verbose)
            verbose=true
            shift
            ;;
        -o|--output)
            output="$2"
            shift 2
            ;;
        -n|--count)
            count="$2"
            shift 2
            ;;
        -h|--help)
            usage
            ;;
        -*)
            echo "Unknown option: $1"
            usage
            ;;
        *)
            input_file="$1"
            shift
            ;;
    esac
done

[[ -z "${input_file:-}" ]] && usage

Reading Files Line by Line

bash
while IFS= read -r line; do
    # Process each line
    echo "Processing: $line"
done < "$input_file"

# Skip blank lines and comments
while IFS= read -r line; do
    [[ -z "$line" || "$line" == \#* ]] && continue
    echo "$line"
done < config.txt

Finding and Processing Files

bash
# find + xargs (handles spaces in filenames)
find . -name "*.log" -print0 | xargs -0 rm -f

# find -exec
find . -name "*.sh" -exec chmod +x {} \;

# find with multiple actions
find /tmp -type f -name "*.tmp" -mtime +7 -delete

Checking Command Existence

bash
if command -v docker &>/dev/null; then
    echo "Docker is installed"
else
    echo "Docker is not installed"
    exit 1
fi

Conditional Execution

bash
# AND — run second command only if first succeeds
mkdir -p build && cd build

# OR — run second command only if first fails
command -v git &>/dev/null || sudo apt install git -y

# Combined
test -f config.yml && echo "Config found" || echo "Config missing"

Text Processing

grep — Search for Patterns

bash
grep "error" logfile.txt              # Lines containing "error"
grep -i "error" logfile.txt           # Case-insensitive
grep -r "TODO" src/                   # Recursive search
grep -n "function" script.sh          # Show line numbers
grep -c "error" logfile.txt           # Count matches
grep -v "debug" logfile.txt           # Invert match (exclude)
grep -E "error|warning" logfile.txt   # Extended regex (OR)
grep -l "pattern" *.txt              # List filenames only

sed — Stream Editor (Find/Replace)

bash
sed 's/old/new/' file.txt             # Replace first occurrence per line
sed 's/old/new/g' file.txt            # Replace all occurrences
sed -i 's/old/new/g' file.txt         # Edit file in place
sed -n '10,20p' file.txt              # Print lines 10-20
sed '/^#/d' file.txt                  # Delete comment lines
sed -i.bak 's/old/new/g' file.txt    # In-place with backup

awk — Columnar Data Processing

bash
awk '{print $1}' file.txt             # Print first column
awk '{print $1, $3}' file.txt         # Print columns 1 and 3
awk -F: '{print $1}' /etc/passwd      # Custom delimiter
awk '$3 > 100' data.txt               # Filter rows
awk '{sum += $1} END {print sum}'     # Sum a column
awk 'NR==1 || $2 > 50' data.txt      # Header + filtered rows

Other Text Tools

bash
cut -d',' -f1,3 data.csv             # Extract CSV columns 1 and 3
sort file.txt                         # Sort lines alphabetically
sort -n file.txt                      # Sort numerically
sort -u file.txt                      # Sort and remove duplicates
uniq file.txt                         # Remove adjacent duplicates
uniq -c file.txt                      # Count adjacent duplicates
wc -l file.txt                        # Count lines
wc -w file.txt                        # Count words
tr 'a-z' 'A-Z' < file.txt           # Convert to uppercase
tr -d '\r' < dos.txt > unix.txt      # Remove carriage returns
head -20 file.txt                     # First 20 lines
tail -20 file.txt                     # Last 20 lines
tail -f logfile.txt                   # Follow file (live updates)

Process Management

bash
# Run a command in the background
long_running_task &

# List background jobs
jobs

# Bring job to foreground
fg %1

# Send job to background
bg %1

# Wait for background processes
wait          # Wait for all
wait $pid     # Wait for specific PID

# Kill a process
kill $pid          # Send SIGTERM (graceful)
kill -9 $pid       # Send SIGKILL (force)
kill %1            # Kill job number 1

# Run immune to hangups
nohup long_task &> output.log &

# Trap signals for cleanup
trap 'echo "Caught SIGINT"; exit 1' INT
trap 'cleanup' EXIT TERM

Shell Configuration

File Loading Order

Bash:

  • Login shell: /etc/profile -> ~/.bash_profile -> ~/.bash_login -> ~/.profile
  • Interactive non-login: ~/.bashrc
  • Non-interactive: $BASH_ENV

Zsh:

  • All: ~/.zshenv
  • Login: ~/.zprofile -> ~/.zshrc -> ~/.zlogin
  • Interactive: ~/.zshrc

Common Configuration

bash
# ~/.bashrc or ~/.zshrc

# PATH management
export PATH="$HOME/.local/bin:$HOME/bin:$PATH"

# Aliases
alias ll='ls -lah'
alias gs='git status'
alias gd='git diff'
alias dc='docker compose'
alias k='kubectl'

# Functions
mkcd() {
    mkdir -p "$1" && cd "$1"
}

# Prompt customization (Bash)
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

# History settings
export HISTSIZE=10000
export HISTFILESIZE=20000
export HISTCONTROL=ignoredups:erasedups
shopt -s histappend    # Bash-specific

Shellcheck

ShellCheck is a static analysis tool for shell scripts that catches common bugs and pitfalls.

Installation

bash
# macOS
brew install shellcheck

# Ubuntu/Debian
sudo apt install shellcheck

# Arch
sudo pacman -S shellcheck

Usage

bash
# Check a script
shellcheck myscript.sh

# Check with specific shell
shellcheck --shell=bash myscript.sh

# Exclude specific warnings
shellcheck --exclude=SC2034 myscript.sh

Common ShellCheck Warnings

CodeIssueFix
SC2086Double quote to prevent globbingUse "$var" instead of $var
SC2046Quote to prevent word splittingUse "$(command)"
SC2034Variable appears unusedRemove or export it
SC2155Declare and assign separatelylocal var; var=$(cmd)
SC2162read without -r mangles backslashesUse read -r

CI Integration

yaml
# GitHub Actions example
- name: Run ShellCheck
  uses: ludeeus/action-shellcheck@master
  with:
    scandir: './scripts'

Best Practices

  1. Always use set -euo pipefail — Exit on errors (-e), treat undefined variables as errors (-u), and fail on any command in a pipeline (-o pipefail).
  2. Quote your variables — Always use "$var" instead of $var to prevent word splitting and globbing issues.
  3. Use ShellCheck — Run shellcheck on every script to catch common bugs before they cause problems in production.
  4. Prefer [[ ]] over [ ] — Double brackets are a Bash/Zsh built-in with better syntax, regex support, and no word splitting issues.
  5. Use functions for reusability — Break scripts into functions with local variables to avoid polluting the global namespace.
  6. Add #!/usr/bin/env bash shebang — The env form is more portable across systems where Bash may not be at /bin/bash.
  7. Trap for cleanup — Use trap cleanup EXIT to ensure temporary files are removed and resources are released, even on errors.
  8. Avoid parsing ls output — Use globs (for f in *.txt) or find instead of parsing ls, which breaks on filenames with spaces or special characters.