developing-posix-shell-scripts skill
This skill defines mandatory guidelines for generating, modifying, or reviewing POSIX-compliant shell scripts (/bin/sh) to ensure portability across different UNIX-like systems and standard compliance.
It MUST be applied whenever strict POSIX compliance is required or when targeting /bin/sh.
Shell Version & Compatibility
- •Scripts MUST be compliant with the POSIX shell standard (IEEE Std 1003.1).
- •Shebang MUST be
#!/bin/sh(or#!/usr/bin/env shif specific environment requirements exist). - •Forbidden Bash/Zsh Features:
- •DO NOT use arrays (
declare -a,name[0]). - •DO NOT use
[[ ... ]](use[ ... ]). - •DO NOT use
function name { ... }syntax (usename() { ... }). - •DO NOT use the
localkeyword (variables are global; use subshells or prefixed names if scope isolation is needed). - •DO NOT use
source(use.instead). - •DO NOT use
<<<(here-strings). - •DO NOT use
<(...)or>(...)(process substitution). - •DO NOT use
letor C-style((...))arithmetic (use$((...))). - •DO NOT use
pipefail. - •DO NOT use
declareortypeset. - •DO NOT use substitution modifiers like
${var: -1}or${var^^}(usetr,sed, or other utilities).
- •DO NOT use arrays (
Control Flow & Logic
- •Guard Clauses: When using
if...elseconstructs or validation logic, always check the incorrect/failure condition first and return/exit early. - •Use
[ ... ]for conditional tests. Ensure variables inside are quoted:[ "$var" = "value" ]. - •Use
casestatements for pattern matching or multiple branches. - •Group related logic into well-named functions using standard syntax
name() { ... }. - •Command Substitution: Use
$(...)instead of backticks`...`.
Variables & Style
- •Variable Expansion: Always use
${var}for variable expansion (including positional parameters like${1}). - •Quoting: Quote all variable expansions (
"${var}") to prevent unintended word splitting and globbing. - •Naming: Use descriptive variable names. Since
localis not standard, consider namespacing function variables (e.g.,_func_var) to avoid collisions.
Error Handling & Robustness
- •Enable strict error handling at the start:
set -o errexit(orset -e) andset -o nounset(orset -u). - •Explicitly verify command exit codes where strictly necessary.
- •Use
printfinstead ofechofor predictable output, especially when printing variable content. - •Log meaningful messages to
stderr.
Tooling & Maintenance
- •Argument Parsing: Use
getopts(built-in) for argument parsing. Note thatgetoptssupports short options only by standard. - •Validation:
- •Validate all scripts using
shellcheck(ensure it targetssh, e.g., via directive#!/bin/sh).
- •Validate all scripts using
- •Format scripts consistently.
Template Script
- •All generated or reviewed scripts MUST be based on the provided template.sh.
- •When defining arguments:
- •Fixed options (
-h) MUST appear first in usage/help. - •Script-specific arguments follow.
- •Fixed options (