Agent skill
shell-patterns
Shell scripting patterns - portability, explicit error handling, readability over cleverness
Install this agent skill to your Project
npx add-skill https://github.com/maxbeworks/dotfiles/tree/main/opencode/.config/opencode/skill/shell-scripting
SKILL.md
Philosophy
- Shell scripts should be obvious
- Portability matters - your script will run on systems you don't control
- Explicit error handling - scripts shouldn't silently fail halfway through
Shebang and Compatibility
- Use
#!/usr/bin/env bashfor portability, not#!/bin/bash - If you need bash-specific features, use bash - don't try to make it POSIX when it's not
- For truly portable scripts, stick to POSIX sh and test on different shells
- Target systems will always be Linux by default unless explicitly told otherwise
Functions
- Use functions for repeated logic - don't copy-paste the same shit
- Name functions clearly -
backup_confignotbc - Keep functions focused - one job per function
- Return meaningful exit codes (0 for success, non-zero for failure)
Variables
- Use
"${var}"instead of$var- quote everything unless you want word splitting - UPPER_CASE for environment/global vars, lower_case for local/function vars
localkeyword for function variables - don't pollute global scope- Check if vars are set before using:
${var:-default}or explicit checks
Commands and Tools
- Never assume a non-default tool exists - ask user before
- Prefer common POSIX tools over GNU-specific extensions when possible
- Use command substitution
$(command)instead of backticks - it's more readable
Loops and Arrays
- Use
while readfor processing command output line by line - Arrays in bash:
arr=("one" "two")- but remember they're bash-specific - Loop over files safely:
for file in *.txt; do [[ -e "$file" ]] || continue - Avoid parsing
lsoutput - use globs orfindwith-print0andwhile read -d ''
Output and Logging
- Use
echofor simple output,printfwhen you need formatting - stderr for errors:
echo "Error message" >&2
Anti-Patterns
- Not quoting variables - guaranteed to break with spaces
- Parsing
lsoutput instead of using proper tools - Ignoring error codes and letting scripts continue after failures
- Using
cdwithout checking if it succeeded - Not cleaning up temporary files
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
ansible-patterns
Ansible playbook patterns - simplicity, external files, system compatibility
python-patterns
Python coding style - pragmatic, readable, fast development over heavy abstraction
typescript-patterns
TypeScript coding style and patterns - type safety, error handling, code organization
nuxt-patterns
Nuxt development patterns - validation, error handling, code organization, state management
obsidian-vault
Search, create, and manage notes in the Obsidian vault with wikilinks and index notes. Use when user wants to find, create, or organize notes in Obsidian.
edit-article
Edit and improve articles by restructuring sections, improving clarity, and tightening prose. Use when user wants to edit, revise, or improve an article draft.
Didn't find tool you were looking for?