shell-best-practices
Use when writing shell scripts following modern best practices. Covers portable scripting, Bash patterns, error handling, and secure coding.
What this skill does
# Shell Scripting Best Practices
Comprehensive guide to writing robust, maintainable, and secure shell scripts following modern best practices.
## Script Foundation
### Shebang Selection
Choose the appropriate shebang for your needs:
```bash
# Portable bash (recommended)
#!/usr/bin/env bash
# Direct bash path (faster, less portable)
#!/bin/bash
# POSIX-compliant shell (most portable)
#!/bin/sh
# Specific shell version
#!/usr/bin/env bash
# Requires Bash 4.0+
```
### Strict Mode
Always enable strict error handling:
```bash
#!/usr/bin/env bash
set -euo pipefail
# What these do:
# -e: Exit immediately on command failure
# -u: Treat unset variables as errors
# -o pipefail: Pipeline fails if any command fails
```
For debugging, add:
```bash
set -x # Print commands as they execute
```
### Script Header Template
```bash
#!/usr/bin/env bash
set -euo pipefail
# Script: script-name.sh
# Description: Brief description of what this script does
# Usage: ./script-name.sh [options] <arguments>
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_NAME="$(basename "${BASH_SOURCE[0]}")"
```
## Variable Handling
### Always Quote Variables
Prevents word splitting and glob expansion:
```bash
# Good
echo "$variable"
cp "$source" "$destination"
if [ -f "$file" ]; then
# Bad - can break on spaces/special chars
echo $variable
cp $source $destination
if [ -f $file ]; then
```
### Use Meaningful Names
```bash
# Good
readonly config_file="/etc/app/config.yml"
local user_input="$1"
declare -a log_files=()
# Bad
readonly f="/etc/app/config.yml"
local x="$1"
declare -a arr=()
```
### Default Values
```bash
# Use default if unset
name="${NAME:-default_value}"
# Use default if unset or empty
name="${NAME:-}"
# Assign default if unset
: "${NAME:=default_value}"
# Error if unset (with message)
: "${REQUIRED_VAR:?Error: REQUIRED_VAR must be set}"
```
### Readonly and Local
```bash
# Constants
readonly MAX_RETRIES=3
readonly CONFIG_DIR="/etc/myapp"
# Function-local variables
my_function() {
local input="$1"
local result=""
# ...
}
```
## Error Handling
### Exit Codes
Use meaningful exit codes:
```bash
# Standard codes
readonly EXIT_SUCCESS=0
readonly EXIT_FAILURE=1
readonly EXIT_INVALID_ARGS=2
readonly EXIT_NOT_FOUND=3
# Exit with code
exit "$EXIT_FAILURE"
```
### Trap for Cleanup
```bash
cleanup() {
local exit_code=$?
# Clean up temporary files
rm -f "${temp_file:-}"
# Restore state if needed
exit "$exit_code"
}
trap cleanup EXIT
# Script continues...
temp_file=$(mktemp)
```
### Error Messages
```bash
error() {
echo "ERROR: $*" >&2
}
warn() {
echo "WARNING: $*" >&2
}
die() {
error "$@"
exit 1
}
# Usage
[[ -f "$config_file" ]] || die "Config file not found: $config_file"
```
### Validate Inputs
```bash
validate_args() {
if [[ $# -lt 1 ]]; then
die "Usage: $SCRIPT_NAME <input_file>"
fi
local input_file="$1"
[[ -f "$input_file" ]] || die "File not found: $input_file"
[[ -r "$input_file" ]] || die "File not readable: $input_file"
}
```
## Functions
### Function Definition
```bash
# Document functions
# Process a log file and extract errors
# Arguments:
# $1 - Path to log file
# $2 - Output directory (optional, default: ./output)
# Returns:
# 0 on success, 1 on failure
process_log() {
local log_file="$1"
local output_dir="${2:-./output}"
[[ -f "$log_file" ]] || return 1
grep -i "error" "$log_file" > "$output_dir/errors.log"
}
```
### Return Values
```bash
# Return status
is_valid() {
[[ -n "$1" && "$1" =~ ^[0-9]+$ ]]
}
if is_valid "$input"; then
echo "Valid"
fi
# Capture output
get_config_value() {
local key="$1"
grep "^${key}=" "$config_file" | cut -d= -f2
}
value=$(get_config_value "database_host")
```
## Conditionals
### Use [[ ]] for Tests
```bash
# Good - [[ ]] is more powerful and safer
if [[ -f "$file" ]]; then
if [[ "$string" == "value" ]]; then
if [[ "$string" =~ ^[0-9]+$ ]]; then
# Avoid - [ ] has limitations
if [ -f "$file" ]; then
if [ "$string" = "value" ]; then
```
### Numeric Comparisons
```bash
# Use (( )) for arithmetic
if (( count > 10 )); then
if (( a == b )); then
if (( x >= 0 && x <= 100 )); then
# Or -eq/-lt/-gt in [[ ]]
if [[ "$count" -gt 10 ]]; then
```
### String Comparisons
```bash
# Equality
if [[ "$str" == "value" ]]; then
# Pattern matching
if [[ "$str" == *.txt ]]; then
# Regex matching
if [[ "$str" =~ ^[a-z]+$ ]]; then
# Empty/non-empty
if [[ -z "$str" ]]; then # empty
if [[ -n "$str" ]]; then # non-empty
```
## Loops
### Iterate Over Files
```bash
# Good - handles spaces in filenames
for file in *.txt; do
[[ -e "$file" ]] || continue # Skip if no matches
process "$file"
done
# With find for recursive
while IFS= read -r -d '' file; do
process "$file"
done < <(find . -name "*.txt" -print0)
# Bad - breaks on spaces
for file in $(ls *.txt); do # Don't do this
```
### Read Lines from File
```bash
# Correct - preserves whitespace
while IFS= read -r line; do
echo "$line"
done < "$filename"
# With process substitution
while IFS= read -r line; do
echo "$line"
done < <(some_command)
```
### Iterate with Index
```bash
files=("one.txt" "two.txt" "three.txt")
for i in "${!files[@]}"; do
echo "Index $i: ${files[i]}"
done
```
## Arrays
### Declaration and Usage
```bash
# Indexed array
declare -a files=()
files+=("file1.txt")
files+=("file2.txt")
# Access all elements
for f in "${files[@]}"; do
echo "$f"
done
# Array length
echo "${#files[@]}"
# Associative array (Bash 4+)
declare -A config
config[host]="localhost"
config[port]="8080"
echo "${config[host]}"
```
### Array Best Practices
```bash
# Quote expansions
"${array[@]}" # All elements, word-split
"${array[*]}" # All elements, single string
# Check if empty
if [[ ${#array[@]} -eq 0 ]]; then
echo "Empty array"
fi
# Check for key (associative)
if [[ -v config[key] ]]; then
echo "Key exists"
fi
```
## Command Execution
### Check Command Existence
```bash
# Preferred method
if command -v docker &>/dev/null; then
echo "Docker is installed"
fi
# In conditionals
require_command() {
command -v "$1" &>/dev/null || die "Required command not found: $1"
}
require_command git
require_command docker
```
### Capture Output and Status
```bash
# Capture output
output=$(some_command)
# Capture output and status
if output=$(some_command 2>&1); then
echo "Success: $output"
else
echo "Failed: $output" >&2
fi
# Check status without output
if some_command &>/dev/null; then
echo "Command succeeded"
fi
```
### Safe Command Substitution
```bash
# Use $() not backticks
result=$(command) # Good
result=`command` # Avoid
# Nested substitution
result=$(echo $(date)) # Works with $()
```
## Portability
### POSIX vs Bash
| Feature | POSIX | Bash |
|---------|-------|------|
| Test syntax | `[ ]` | `[[ ]]` |
| Arrays | No | Yes |
| `$()` | Yes | Yes |
| `${var//pat/rep}` | No | Yes |
| `[[ =~ ]]` regex | No | Yes |
| `(( ))` arithmetic | No | Yes |
### Portable Alternatives
```bash
# Instead of [[ ]], use [ ] with quotes
if [ -f "$file" ]; then
if [ "$str" = "value" ]; then
# Instead of (( )), use [ ] with -eq
if [ "$count" -gt 10 ]; then
# Instead of ${var//pat/rep}
echo "$var" | sed 's/pat/rep/g'
# Instead of arrays, use space-separated strings
files="one.txt two.txt three.txt"
for f in $files; do
echo "$f"
done
```
## Security
### Avoid Eval
```bash
# Bad - code injection risk
eval "$user_input"
# Better - use arrays for command building
cmd=("grep" "-r" "$pattern" "$directory")
"${cmd[@]}"
```
### Sanitize Inputs
```bash
# Validate expected format
if [[ ! "$input" =~ ^[a-zA-Z0-9_-]+$ ]]; then
die "Invalid input format"
fi
# Escape for use in commands
escaped=$(printf '%q' "$input")
```
### Temporary Files
```bash
# Secure temp file creation
temp_file=$(mktemp) || die "Failed to create temp file"
trapRelated in Writing & Docs
jax-development
IncludedUse this skill when the user is writing, debugging, profiling, refactoring, reviewing, benchmarking, parallelising, exporting, or explaining JAX code, or when they mention JAX, jax.numpy, jit, grad, value_and_grad, vmap, scan, lax, random keys, pytrees, jax.Array, sharding, Mesh, PartitionSpec, NamedSharding, pmap, shard_map, Pallas, XLA, StableHLO, checkify, profiler, or the JAX repo. It helps turn NumPy or PyTorch-style code into pure functional JAX, fix tracer/control-flow/shape/PRNG bugs, remove recompiles and host-device syncs, choose transforms and sharding strategies, inspect jaxpr/lowering/IR, and benchmark compiled code correctly.
nature-article-writer
IncludedDrafts, rewrites, diagnostically critiques, and style-calibrates primary research manuscripts for Nature and Nature Portfolio journals. Use when the user wants a Nature-style title, summary paragraph or abstract, introduction, results, discussion, methods, figure legends, presubmission enquiry, cover letter, reviewer response, or when a scientific draft sounds generic, jargon-heavy, structurally weak, or AI-ish and needs precise, broad-reader-friendly prose without inventing data, analyses, or references. Best for primary research articles and letters rather than reviews or press releases unless explicitly adapting one.
deckrd
IncludedDocument-driven framework that derives requirements, specifications, implementation plans, and executable tasks from goals through structured AI dialogue. Use when user says "write requirements", "create spec", "plan implementation", "derive tasks", "structure this feature", "break down into tasks", or "document this module". Also use for reverse engineering existing code into docs (/deckrd rev). Do NOT use for direct code writing — use /deckrd-coder after tasks are generated. Do NOT use when the user only wants to run or fix existing code without planning.
clinical-decision-support
IncludedGenerate professional clinical decision support (CDS) documents for pharmaceutical and clinical research settings, including patient cohort analyses (biomarker-stratified with outcomes) and treatment recommendation reports (evidence-based guidelines with decision algorithms). Supports GRADE evidence grading, statistical analysis (hazard ratios, survival curves, waterfall plots), biomarker integration, and regulatory compliance. Outputs publication-ready LaTeX/PDF format optimized for drug development, clinical research, and evidence synthesis.
handling-sf-data
IncludedSalesforce data operations with 130-point scoring. Use this skill to create, update, delete, bulk import/export, generate test data, and clean up org records using sf CLI and anonymous Apex. TRIGGER when: user creates test data, performs bulk import/export, uses sf data CLI commands, needs data factory patterns for Apex tests, or needs to seed/clean records in a Salesforce org. DO NOT TRIGGER when: SOQL query writing only (use querying-soql), Apex test execution (use running-apex-tests), or metadata deployment (use deploying-metadata).
accelint-ac-to-playwright
IncludedConvert and validate acceptance criteria for Playwright test automation. Use when user asks to (1) review/evaluate/check if AC are ready for automation, (2) assess if AC can be converted as-is, (3) validate AC quality for Playwright, (4) turn AC into tests, (5) generate tests from acceptance criteria, (6) convert .md bullets or .feature Gherkin files to Playwright specs, (7) create test automation from requirements. Handles both bullet-style markdown and Gherkin syntax with JSON test plan generation and validation.