Bash Scripting Mastery
Scope and platform contract
This skill targets Bash itself, wherever Bash runs - Linux, macOS, WSL, Git Bash / MSYS2 on Windows, and Bash-based container images. It does not cover native PowerShell: a PowerShell script is a different language and should use powershell-master. On Windows, bash-master assumes the user is running Bash inside Git Bash, WSL, or a similar Bash environment, and addresses the MSYS path-translation quirks that result.
Repository conventions
Project-level conventions (Windows backslashes in tool calls, documentation discipline, etc.) live in the agent body and the windows-path-master plugin. This skill focuses on Bash content; do not duplicate that boilerplate here.
Quick reference
Bash-portability quick check:
When to use this skill
Always activate for:
- Writing or modifying any bash/shell script
- Reviewing or refactoring existing scripts
- Debugging shell script failures
- DevOps automation, CI/CD pipelines, system administration
- Cross-environment Bash portability (Linux <-> macOS <-> WSL <-> Git Bash <-> container)
Do not use this skill for:
- PowerShell scripts - use
powershell-master - Batch (
.cmd/.bat) scripting - Generic command help unrelated to scripting
Core principles
1. Safety first
Every script should open with the safety preamble:
2. POSIX vs Bash
3. Quoting
4. ShellCheck
Run shellcheck on every script. Only disable warnings with a justification comment: # shellcheck disable=SC2086 reason: intentional word splitting.
See references/best_practices.md for the full quoting/style table and references/patterns_antipatterns.md for the common pitfalls.
Platform-specific considerations
Git Bash / MSYS2 (Windows)
Git Bash auto-converts Unix-style arguments to Windows paths. This is the largest single source of cross-platform Bash bugs on Windows.
Full Git Bash + Windows path notes live in references/windows-git-bash-paths.md.
Linux
GNU coreutils, /proc, systemd integration. Detect via [[ "$OSTYPE" == "linux-gnu"* ]].
macOS
BSD utilities behave differently from GNU. Most common gotchas: sed -i '' (empty string required), date flags differ, readlink -f not available on stock macOS.
WSL
Effectively Linux. The Windows filesystem is mounted at /mnt/c/. Detect via grep -qi microsoft /proc/version.
Containers
Alpine images ship only /bin/sh (BusyBox). Write POSIX-compliant scripts or apk add bash. Container init quirks: PID 1 must reap children and handle signals. Detect via [ -f /.dockerenv ] or [ -n "$KUBERNETES_SERVICE_HOST" ].
Portable platform-detection template
Full per-platform tables (BSD-vs-GNU coreutils flags, WSL networking, container init patterns) live in references/platform_specifics.md.
Best practices (summary)
The full patterns - function design, error handling, input validation, argument parsing, logging - live in references/in-depth-patterns.md. The headline rules:
- One concern per function; locals declared first; validate input; return non-zero on error.
- Constants
UPPER_CASE; localslower_case; mark immutable valuesreadonly. - Always check exit codes (
if ! cmd,||, traps, or a centralerror_exithelper). - Validate every external input - empty, format, length, charset.
- Use
getoptsor acase-based argument parser; print usage and exit 1 on bad input. - Use a leveled logger that writes to stderr.
Security, performance, testing, debugging, advanced patterns
These each have dedicated sections in references/in-depth-patterns.md:
Read that reference any time you need the canonical code template for one of those topics.
Reference files
references/platform_specifics.md[blocked] - Detailed platform differences and workaroundsreferences/best_practices.md[blocked] - Comprehensive industry standards and guidelinesreferences/patterns_antipatterns.md[blocked] - Common patterns and pitfalls with solutionsreferences/windows-git-bash-paths.md[blocked] - Git Bash / MSYS path-translation referencereferences/in-depth-patterns.md[blocked] - Function design, security, performance, testing, debugging, advanced patternsreferences/resources.md[blocked] - Official docs, style guides, tooling, and learning links
Success criteria
A Bash script written with this skill should:
- Pass
shellcheckwith no warnings - Begin with
set -euo pipefail - Quote every variable expansion
- Print usage on
-h/--help - Decompose into testable functions
- Handle empty input, missing files, and unexpected arguments
- Run on every target platform (Linux/macOS/WSL/Git Bash/container) where it claims support
- Match the Google Shell Style Guide
- Clean up on exit (
trap EXIT) - Be unit-tested with BATS where logic is non-trivial
Troubleshooting
Script fails on a different platform
checkbashisms script.shto surface non-portable constructs.command -v toolto verify a required tool is installed.- Diff command flags between GNU and BSD (
sed --versionetc.).
ShellCheck warnings
- Read the rule explanation (
shellcheck -W SC2086). - Fix the underlying issue; only disable a rule with a justification comment.
Works interactively but fails in cron
- Cron has a minimal
PATH- setPATHexplicitly. - Use absolute paths.
- Redirect stdout/stderr:
./script.sh >> /tmp/cron.log 2>&1.
Performance issues
- Profile with
time. - Enable
set -xto find slow steps. - Replace external invocations with Bash built-ins where possible.


