bash-development
Bash Development
Core patterns and best practices for Bash 5.1+ script development. Provides modern bash idioms, error handling, argument parsing, and pure-bash alternatives to external commands.
Script Foundation
Every script starts with the essential header:
#!/usr/bin/env bash
set -euo pipefail
set options explained:
-e- Exit immediately on command failure-u- Treat unset variables as errors-o pipefail- Pipeline fails if any command fails
Script Metadata Pattern
SCRIPT_NAME=$(basename "${BASH_SOURCE[0]}")
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
readonly SCRIPT_VERSION="1.0.0"
readonly SCRIPT_NAME SCRIPT_DIR
Error Handling
Implement trap-based error handling for robust scripts.
Argument Parsing
Standard argument parsing template.
Variable Best Practices
Always use curly braces and quote variables:
# Correct
"${variable}"
"${array[@]}"
# Incorrect
$variable
${array[*]} # Use [@] for proper iteration
Use readonly for constants:
readonly CONFIG_FILE="/etc/app/config"
readonly -a VALID_OPTIONS=("opt1" "opt2" "opt3")
Note: Never use readonly in sourced scripts - it causes errors on re-sourcing.
String Operations (Pure Bash)
Prefer native bash parameter expansion over external tools:
# Trim whitespace
trimmed="${string#"${string%%[![:space:]]*}"}"
trimmed="${trimmed%"${trimmed##*[![:space:]]}"}"
# Lowercase/Uppercase (Bash 4+)
lower="${string,,}"
upper="${string^^}"
# Substring extraction
substring="${string:0:10}" # First 10 chars
suffix="${string: -5}" # Last 5 chars
# Replace patterns
replaced="${string//old/new}" # Replace all
replaced="${string/old/new}" # Replace first
# Strip prefix/suffix
no_prefix="${string#prefix}" # Shortest match
no_prefix="${string##*/}" # Longest match (basename)
no_suffix="${string%suffix}" # Shortest match
no_suffix="${string%%/*}" # Longest match
Array Operations
# Declaration
declare -a indexed_array=()
declare -A assoc_array=()
# Safe iteration with nullglob
shopt -s nullglob
for file in *.txt; do
process "${file}"
done
shopt -u nullglob
# Array length
length="${#array[@]}"
# Append element
array+=("new_element")
# Iterate with index
for i in "${!array[@]}"; do
printf '%d: %s\n' "${i}" "${array[i]}"
done
File Operations
# Read file to string
content="$(<"${file}")"
# Read file to array (Bash 4+)
mapfile -t lines < "${file}"
# Check file conditions
[[ -f "${file}" ]] # Regular file exists
[[ -d "${dir}" ]] # Directory exists
[[ -r "${file}" ]] # Readable
[[ -w "${file}" ]] # Writable
[[ -x "${file}" ]] # Executable
[[ -s "${file}" ]] # Non-empty
# Safe temp file creation
temp_file=$(mktemp)
trap 'rm -f "${temp_file}"' EXIT
Conditional Expressions
Use [[ ]] for conditionals (bash-specific, more powerful):
# String comparisons
[[ "${var}" == "value" ]] # Equality
[[ "${var}" == pattern* ]] # Glob matching
[[ "${var}" =~ ^regex$ ]] # Regex matching
# Numeric comparisons
(( num > 10 )) # Arithmetic comparison
[[ "${num}" -gt 10 ]] # Traditional syntax
# Compound conditions
[[ -f "${file}" && -r "${file}" ]]
[[ "${opt}" == "a" || "${opt}" == "b" ]]
Utility Functions
Performance Guidelines
- Use builtins over external commands when possible
- Batch operations instead of loops for large datasets
- Use
printfoverechofor portability and control - Avoid unnecessary subshells in tight loops
- Use
[[ ]]over[ ]for string comparisons
Additional Resources
Reference Files
For detailed patterns and examples:
- bash_example_file.sh - Complete script template
- bash_example_includes.bash - Reusable utility functions
- bash-agent-notes.markdown - Context-aware review guidance
- pure-bash-bible-strings.md - String manipulation patterns
- pure-bash-bible-arrays.md - Array operations
- pure-bash-bible-files.md - File handling patterns
- pure-bash-bible-variables.md - Parameter expansion reference
More from jamie-bitflight/claude_skills
perl-lint
This skill should be used when the user asks to lint Perl code, run perlcritic, check Perl style, format Perl code, run perltidy, or mentions Perl Critic policies, code formatting, or style checking.
24brainstorming-skill
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or when users request help with ideation, marketing, and strategic planning. Explores user intent, requirements, and design before implementation using 30+ research-validated prompt patterns.
11design-anti-patterns
Enforce anti-AI UI design rules based on the Uncodixfy methodology. Use when generating HTML, CSS, React, Vue, Svelte, or any frontend UI code. Prevents "Codex UI" — the generic AI aesthetic of soft gradients, floating panels, oversized rounded corners, glassmorphism, hero sections in dashboards, and decorative copy. Applies constraints from Linear/Raycast/Stripe/GitHub design philosophy: functional, honest, human-designed interfaces. Triggers on: UI generation, dashboard building, frontend component creation, CSS styling, landing page design, or any task producing visual interface code.
7python3-review
Comprehensive Python code review checking patterns, types, security, and performance. Use when reviewing Python code for quality issues, when auditing code before merge, or when assessing technical debt in a Python codebase.
7hooks-guide
Cross-platform hooks reference for AI coding assistants — Claude Code, GitHub Copilot, Cursor, Windsurf, Amp. Covers hook authoring in Node.js CJS and Python, per-platform event schemas, inline-agent hooks and MCP in agent frontmatter, common JSON I/O, exit codes, best practices, and a fetch script to refresh docs from official sources. Use when writing, reviewing, or debugging hooks for any AI assistant.
7agent-creator
Create high-quality Claude Code agents from scratch or by adapting existing agents as templates. Use when the user wants to create a new agent, modify agent configurations, build specialized subagents, or design agent architectures. Guides through requirements gathering, template selection, and agent file generation following Anthropic best practices (v2.1.63+).
6