Refactor shell scripts for clarity, performance, and maintainability without changing behavior — use for 'optimize script', 'refactor bash', 'improve shell script', 'clean up script'
Install
npx skillscat add cuozg/oh-my-unity/bash-optimize Install via the SkillsCat registry.
SKILL.md
bash-optimize
Refactor an existing shell script to improve readability, reduce complexity, and apply best practices — behavior must not change.
When to Use
- A working script has grown hard to read or maintain
- Performance bottlenecks exist (e.g., unnecessary subshells, repeated calls)
- Script lacks
set -euo pipefailor proper quoting - Preparing a script for team ownership or long-term maintenance
Workflow
- Read — Read the full script and understand its intended behavior
- Identify — List specific issues: quoting gaps, useless forks, repeated code, poor naming, missing guards
- Refactor — Apply improvements: extract functions, fix quoting, add
set -euo pipefail, replace anti-patterns - Verify — Confirm the refactored script produces identical behavior for all identified execution paths
- Report — List every change made with before/after examples
Rules
- Never change observable behavior — refactor only
- Apply
set -euo pipefailat the top unless the script explicitly requires lenient error handling - Quote all variable expansions unless word-splitting is intentional
- Replace
$(cat file)with< fileredirection where appropriate - Extract repeated logic into named functions
- Prefer
[[ ]]over[ ]for conditionals in bash scripts
Output Format
Optimized script (inline replacement) plus a report listing each change, the problem it solved, and the pattern applied.