|
| 1 | +--- |
| 2 | +description: 'Shell scripting best practices and conventions for bash, sh, zsh, and other shells' |
| 3 | +applyTo: '**/*.sh' |
| 4 | +--- |
| 5 | + |
| 6 | +# Shell Scripting Guidelines |
| 7 | + |
| 8 | +Instructions for writing clean, safe, and maintainable shell scripts for bash, sh, zsh, and other shells. |
| 9 | + |
| 10 | +## General Principles |
| 11 | + |
| 12 | +- Generate code that is clean, simple, and concise |
| 13 | +- Ensure scripts are easily readable and understandable |
| 14 | +- Add comments where helpful for understanding how the script works |
| 15 | +- Generate concise and simple echo outputs to provide execution status |
| 16 | +- Avoid unnecessary echo output and excessive logging |
| 17 | +- Use shellcheck for static analysis when available |
| 18 | +- Assume scripts are for automation and testing rather than production systems unless specified otherwise |
| 19 | +- Prefer safe expansions: double-quote variable references (`"$var"`), use `${var}` for clarity, and avoid `eval` |
| 20 | +- Use modern Bash features (`[[ ]]`, `local`, arrays) when portability requirements allow; fall back to POSIX constructs only when needed |
| 21 | +- Choose reliable parsers for structured data instead of ad-hoc text processing |
| 22 | + |
| 23 | +## Error Handling & Safety |
| 24 | + |
| 25 | +- Always enable `set -euo pipefail` to fail fast on errors, catch unset variables, and surface pipeline failures |
| 26 | +- Validate all required parameters before execution |
| 27 | +- Provide clear error messages with context |
| 28 | +- Use `trap` to clean up temporary resources or handle unexpected exits when the script terminates |
| 29 | +- Declare immutable values with `readonly` (or `declare -r`) to prevent accidental reassignment |
| 30 | +- Use `mktemp` to create temporary files or directories safely and ensure they are removed in your cleanup handler |
| 31 | + |
| 32 | +## Script Structure |
| 33 | + |
| 34 | +- Start with a clear shebang: `#!/bin/bash` unless specified otherwise |
| 35 | +- Include a header comment explaining the script's purpose |
| 36 | +- Define default values for all variables at the top |
| 37 | +- Use functions for reusable code blocks |
| 38 | +- Create reusable functions instead of repeating similar blocks of code |
| 39 | +- Keep the main execution flow clean and readable |
| 40 | + |
| 41 | +## Working with JSON and YAML |
| 42 | + |
| 43 | +- Prefer dedicated parsers (`jq` for JSON, `yq` for YAML—or `jq` on JSON converted via `yq`) over ad-hoc text processing with `grep`, `awk`, or shell string splitting |
| 44 | +- When `jq`/`yq` are unavailable or not appropriate, choose the next most reliable parser available in your environment, and be explicit about how it should be used safely |
| 45 | +- Validate that required fields exist and handle missing/invalid data paths explicitly (e.g., by checking `jq` exit status or using `// empty`) |
| 46 | +- Quote jq/yq filters to prevent shell expansion and prefer `--raw-output` when you need plain strings |
| 47 | +- Treat parser errors as fatal: combine with `set -euo pipefail` or test command success before using results |
| 48 | +- Document parser dependencies at the top of the script and fail fast with a helpful message if `jq`/`yq` (or alternative tools) are required but not installed |
| 49 | + |
| 50 | +```bash |
| 51 | +#!/bin/bash |
| 52 | + |
| 53 | +# ============================================================================ |
| 54 | +# Script Description Here |
| 55 | +# ============================================================================ |
| 56 | + |
| 57 | +set -euo pipefail |
| 58 | + |
| 59 | +cleanup() { |
| 60 | + # Remove temporary resources or perform other teardown steps as needed |
| 61 | + if [[ -n "${TEMP_DIR:-}" && -d "$TEMP_DIR" ]]; then |
| 62 | + rm -rf "$TEMP_DIR" |
| 63 | + fi |
| 64 | +} |
| 65 | + |
| 66 | +trap cleanup EXIT |
| 67 | + |
| 68 | +# Default values |
| 69 | +RESOURCE_GROUP="" |
| 70 | +REQUIRED_PARAM="" |
| 71 | +OPTIONAL_PARAM="default-value" |
| 72 | +readonly SCRIPT_NAME="$(basename "$0")" |
| 73 | + |
| 74 | +TEMP_DIR="" |
| 75 | + |
| 76 | +# Functions |
| 77 | +usage() { |
| 78 | + echo "Usage: $SCRIPT_NAME [OPTIONS]" |
| 79 | + echo "Options:" |
| 80 | + echo " -g, --resource-group Resource group (required)" |
| 81 | + echo " -h, --help Show this help" |
| 82 | + exit 0 |
| 83 | +} |
| 84 | + |
| 85 | +validate_requirements() { |
| 86 | + if [[ -z "$RESOURCE_GROUP" ]]; then |
| 87 | + echo "Error: Resource group is required" |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +main() { |
| 93 | + validate_requirements |
| 94 | + |
| 95 | + TEMP_DIR="$(mktemp -d)" |
| 96 | + if [[ ! -d "$TEMP_DIR" ]]; then |
| 97 | + echo "Error: failed to create temporary directory" >&2 |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | + |
| 101 | + echo "============================================================================" |
| 102 | + echo "Script Execution Started" |
| 103 | + echo "============================================================================" |
| 104 | + |
| 105 | + # Main logic here |
| 106 | + |
| 107 | + echo "============================================================================" |
| 108 | + echo "Script Execution Completed" |
| 109 | + echo "============================================================================" |
| 110 | +} |
| 111 | + |
| 112 | +# Parse arguments |
| 113 | +while [[ $# -gt 0 ]]; do |
| 114 | + case $1 in |
| 115 | + -g|--resource-group) |
| 116 | + RESOURCE_GROUP="$2" |
| 117 | + shift 2 |
| 118 | + ;; |
| 119 | + -h|--help) |
| 120 | + usage |
| 121 | + ;; |
| 122 | + *) |
| 123 | + echo "Unknown option: $1" |
| 124 | + exit 1 |
| 125 | + ;; |
| 126 | + esac |
| 127 | +done |
| 128 | + |
| 129 | +# Execute main function |
| 130 | +main "$@" |
| 131 | + |
| 132 | +``` |
0 commit comments