1---2name: posix-shell-pro3description: Expert in strict POSIX sh scripting for maximum portability across Unix-like systems. Specializes in shell scripts that run on any POSIX-compliant shell (dash, ash, sh, bash --posix). 4category: Document Processing5source: antigravity6tags: [ai, workflow, design, document, security, rag, cro]7url: https://github.com/sickn33/antigravity-awesome-skills/tree/main/skills/posix-shell-pro8metadata:9 source: https://skillmd.com/skills/sickn33/posix-shell-pro10---111213## Use this skill when1415- Working on posix shell pro tasks or workflows16- Needing guidance, best practices, or checklists for posix shell pro1718## Do not use this skill when1920- The task is unrelated to posix shell pro21- You need a different domain or tool outside this scope2223## Instructions2425- Clarify goals, constraints, and required inputs.26- Apply relevant best practices and validate outcomes.27- Provide actionable steps and verification.28- If detailed examples are required, open `resources/implementation-playbook.md`.2930## Focus Areas3132- Strict POSIX compliance for maximum portability33- Shell-agnostic scripting that works on any Unix-like system34- Defensive programming with portable error handling35- Safe argument parsing without bash-specific features36- Portable file operations and resource management37- Cross-platform compatibility (Linux, BSD, Solaris, AIX, macOS)38- Testing with dash, ash, and POSIX mode validation39- Static analysis with ShellCheck in POSIX mode40- Minimalist approach using only POSIX-specified features41- Compatibility with legacy systems and embedded environments4243## POSIX Constraints4445- No arrays (use positional parameters or delimited strings)46- No `[[` conditionals (use `[` test command only)47- No process substitution `<()` or `>()`48- No brace expansion `{1..10}`49- No `local` keyword (use function-scoped variables carefully)50- No `declare`, `typeset`, or `readonly` for variable attributes51- No `+=` operator for string concatenation52- No `${var//pattern/replacement}` substitution53- No associative arrays or hash tables54- No `source` command (use `.` for sourcing files)5556## Approach5758- Always use `#!/bin/sh` shebang for POSIX shell59- Use `set -eu` for error handling (no `pipefail` in POSIX)60- Quote all variable expansions: `"$var"` never `$var`61- Use `[ ]` for all conditional tests, never `[[`62- Implement argument parsing with `while` and `case` (no `getopts` for long options)63- Create temporary files safely with `mktemp` and cleanup traps64- Use `printf` instead of `echo` for all output (echo behavior varies)65- Use `. script.sh` instead of `source script.sh` for sourcing66- Implement error handling with explicit `|| exit 1` checks67- Design scripts to be idempotent and support dry-run modes68- Use `IFS` manipulation carefully and restore original value69- Validate inputs with `[ -n "$var" ]` and `[ -z "$var" ]` tests70- End option parsing with `--` and use `rm -rf -- "$dir"` for safety71- Use command substitution `$()` instead of backticks for readability72- Implement structured logging with timestamps using `date`73- Test scripts with dash/ash to verify POSIX compliance7475## Compatibility & Portability7677- Use `#!/bin/sh` to invoke the system's POSIX shell78- Test on multiple shells: dash (Debian/Ubuntu default), ash (Alpine/BusyBox), bash --posix79- Avoid GNU-specific options; use POSIX-specified flags only80- Handle platform differences: `uname -s` for OS detection81- Use `command -v` instead of `which` (more portable)82- Check for command availability: `command -v cmd >/dev/null 2>&1 || exit 1`83- Provide portable implementations for missing utilities84- Use `[ -e "$file" ]` for existence checks (works on all systems)85- Avoid `/dev/stdin`, `/dev/stdout` (not universally available)86- Use explicit redirection instead of `&>` (bash-specific)8788## Readability & Maintainability8990- Use descriptive variable names in UPPER_CASE for exports, lower_case for locals91- Add section headers with comment blocks for organization92- Keep functions under 50 lines; extract complex logic93- Use consistent indentation (spaces only, typically 2 or 4)94- Document function purpose and parameters in comments95- Use meaningful names: `validate_input` not `check`96- Add comments for non-obvious POSIX workarounds97- Group related functions with descriptive headers98- Extract repeated code into functions99- Use blank lines to separate logical sections100101## Safety & Security Patterns102103- Quote all variable expansions to prevent word splitting104- Validate file permissions before operations: `[ -r "$file" ] || exit 1`105- Sanitize user input before using in commands106- Validate numeric input: `case $num in *[!0-9]*) exit 1 ;; esac`107- Never use `eval` on untrusted input108- Use `--` to separate options from arguments: `rm -- "$file"`109- Validate required variables: `[ -n "$VAR" ] || { echo "VAR required" >&2; exit 1; }`110- Check exit codes explicitly: `cmd || { echo "failed" >&2; exit 1; }`111- Use `trap` for cleanup: `trap 'rm -f "$tmpfile"' EXIT INT TERM`112- Set restrictive umask for sensitive files: `umask 077`113- Log security-relevant operations to syslog or file114- Validate file paths don't contain unexpected characters115- Use full paths for commands in security-critical scripts: `/bin/rm` not `rm`116117## Performance Optimization118119- Use shell built-ins over external commands when possible120- Avoid spawning subshells in loops: use `while read` not `for i in $(cat)`121- Cache command results in variables instead of repeated execution122- Use `case` for multiple st