Skip to content
TheBushidoCollective avatar

jutsu-shellcheck

Shell script validation plugin that runs ShellCheck with portability and error handling guidance.

CCM score
50 of 100
GitHub stars
201
View on GitHub

Own this plugin? Claim it

What it does

  • Adds three skills, shell-scripting-fundamentals, shell-error-handling, and shell-portability, plus a hooks.json validation hook
  • Runs ShellCheck on all .sh files at session stop and after agents finish, catching common bugs and style issues
  • Runs only when shell files have changed, using the --cache flag to skip unchanged scripts
  • Designed for projects that need bash scripts to stay correct and portable across Linux, macOS, and different shells

Validation and quality enforcement for Bash and shell scripts using ShellCheck with portability and error handling skills.

Add marketplace

/plugin marketplace add TheBushidoCollective/han

Run it inside Claude Code once, then install the plugin below.

Install

/plugin install jutsu-shellcheck@han

Run it inside Claude Code after adding the marketplace.

Files

Discovered from source

Shell Error Handling

Patterns for robust error handling, cleanup, and debugging in shell scripts.

Exit Codes

Standard Exit Codes

CodeMeaning
0Success
1General error
2Misuse of shell command
126Command not executable
127Command not found
128+NFatal signal N
130Ctrl+C (SIGINT)

Checking Exit Status

# Check last command's exit status
if ! command; then
    echo "Command failed with status $?" >&2
    exit 1
fi

# Alternative pattern
command || {
    echo "Command failed" >&2
    exit 1
}

# Capture exit status
command
status=$?
if (( status != 0 )); then
    echo "Failed with status $status" >&2
fi

Trap for Cleanup

Basic Cleanup Pattern

#!/usr/bin/env bash
set -euo pipefail

cleanup() {
    local exit_code=$?
    # Remove temporary files
    rm -f "$TEMP_FILE" 2>/dev/null || true
    exit "$exit_code"
}

trap cleanup EXIT

TEMP_FILE=$(mktemp)
# Script continues...
# cleanup runs automatically on exit

Handling Multiple Signals

#!/usr/bin/env bash
set -euo pipefail

cleanup() {
    echo "Cleaning up..." >&2
    rm -rf "$WORK_DIR" 2>/dev/null || true
}

handle_interrupt() {
    echo "Interrupted by user" >&2
    cleanup
    exit 130
}

trap cleanup EXIT
trap handle_interrupt INT TERM

WORK_DIR=$(mktemp -d)

Trap Best Practices

# Preserve original exit code in cleanup
cleanup() {
    local exit_code=$?
    # Cleanup operations here
    rm -f "$temp_file" 2>/dev/null || true
    # Restore exit code
    exit "$exit_code"
}

# Use || true for optional cleanup
trap 'rm -f "$temp_file" 2>/dev/null || true' EXIT

Error Reporting

Standard Error Output

# Always write errors to stderr
echo "Error: Something went wrong" >&2

# Error function
error() {
    echo "Error: $*" >&2
}

# Die function - error and exit
die() {
    echo "Fatal: $*" >&2
    exit 1
}

# Usage
[[ -f "$config" ]] || die "Config file not found: $config"

Verbose Logging

#!/usr/bin/env bash
set -euo pipefail

VERBOSE="${VERBOSE:-false}"

log() {
    if [[ "$VERBOSE" == "true" ]]; then
        echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >&2
    fi
}

error() {
    echo "[ERROR] $*" >&2
}

log "Starting script"
log "Processing file: $file"

Defensive Programming

Check Prerequisites

# Check required commands exist
require_command() {
    command -v "$1" >/dev/null 2>&1 || {
        echo "Error: Required command '$1' not found" >&2
        exit 1
    }
}

require_command jq
require_command curl
require_command shellcheck

Validate Input

# Validate arguments
if [[ $# -lt 2 ]]; then
    echo "Usage: $0 <source> <destination>" >&2
    exit 1
fi

source_file="$1"
dest_dir="$2"

# Validate file exists
[[ -f "$source_file" ]] || {
    echo "Error: Source file not found: $source_file" >&2
    exit 1
}

# Validate directory
[[ -d "$dest_dir" ]] || {
    echo "Error: Destination directory not found: $dest_dir" >&2
    exit 1
}

Safe Temporary Files

# Create secure temp file
TEMP_FILE=$(mktemp) || {
    echo "Error: Failed to create temp file" >&2
    exit 1
}

# Create secure temp directory
TEMP_DIR=$(mktemp -d) || {
    echo "Error: Failed to create temp directory" >&2
    exit 1
}

# Always clean up
trap 'rm -rf "$TEMP_FILE" "$TEMP_DIR" 2>/dev/null || true' EXIT

Debugging

Debug Mode

#!/usr/bin/env bash

# Enable debug mode via environment variable
if [[ "${DEBUG:-}" == "1" ]]; then
    set -x
fi

set -euo pipefail

# Or toggle with a flag
while getopts "d" opt; do
    case $opt in
        d) set -x ;;
        *) echo "Usage: $0 [-d]" >&2; exit 1 ;;
    esac
done

Trace Execution

# Enable tracing for specific section
set -x
problematic_code
set +x

# Trace with custom PS4
export PS4='+ ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -x

Error Recovery Patterns

Retry Pattern

retry() {
    local max_attempts="${1:-3}"
    local delay="${2:-1}"
    shift 2
    local cmd=("$@")

    local attempt=1
    while (( attempt <= max_attempts )); do
        if "${cmd[@]}"; then
            return 0
        fi
        echo "Attempt $attempt failed, retrying in ${delay}s..." >&2
        sleep "$delay"
        (( attempt++ ))
    done

    echo "All $max_attempts attempts failed" >&2
    return 1
}

# Usage
retry 3 5 curl -f "http://example.com/api"

Fallback Pattern

# Try primary, fall back to secondary
get_config() {
    if [[ -f "$HOME/.config/myapp/config" ]]; then
        cat "$HOME/.config/myapp/config"
    elif [[ -f "/etc/myapp/config" ]]; then
        cat "/etc/myapp/config"
    else
        echo "Error: No config file found" >&2
        return 1
    fi
}

Readme

ShellCheck

Validation and quality enforcement for Bash and shell scripts using ShellCheck.

What This Plugin Provides

Validation Hooks

  • ShellCheck Validation: Runs ShellCheck on all .sh files to catch common bugs, pitfalls, and style issues
  • Validates scripts on session stop and when agents complete work
  • Only runs when shell files have changed (with --cache flag)

Skills

This plugin provides the following skills:

  • shell-scripting-fundamentals: Core patterns for variables, conditionals, loops, and functions
  • shell-error-handling: Traps, exit codes, cleanup routines, and debugging techniques
  • shell-portability: Writing scripts that work across Linux, macOS, and different shells

Installation

han plugin install shellcheck

Usage

Once installed, this plugin automatically validates your shell scripts:

  • When you finish a conversation with Claude Code
  • When Claude Code agents complete their work
  • Before commits (when combined with git hooks)

Manual Validation

Run ShellCheck manually on your scripts:

shellcheck -x script.sh
shellcheck -x **/*.sh

Configuring ShellCheck

Create a .shellcheckrc file in your project root:

# Disable specific checks
disable=SC2034,SC2086

# Set default shell
shell=bash

# Enable external sources
external-sources=true

Requirements

  • ShellCheck installed and available in PATH
  • Bash 4.0+ (for the validation hooks)

Installing ShellCheck

macOS:

brew install shellcheck

Ubuntu/Debian:

apt-get install shellcheck

Fedora:

dnf install ShellCheck

From source:

cabal update
cabal install ShellCheck

Common ShellCheck Codes

CodeDescription
SC2086Double quote to prevent globbing and word splitting
SC2046Quote this to prevent word splitting
SC2034Variable appears unused
SC2155Declare and assign separately to avoid masking return values
SC2164Use cd ... || exit in case cd fails
SC2006Use $(...) instead of legacy backticks

Overriding Hooks

Create a han-config.yml in directories where you want to customize behavior:

shellcheck:
  shellcheck:
    enabled: false  # Disable shellcheck for this directory

Or override the command:

shellcheck:
  shellcheck:
    command: "shellcheck -e SC2034 *.sh"  # Ignore unused variable warnings

Details

Marketplace
TheBushidoCollective/han
Source
plugins/validation/shellcheck
Category
Validation
Keywords
bash, shell, shellcheck, scripting, validation, posix
Listed since
Apr 7, 2026

More from TheBushidoCollective/han

All 340

Similar to jutsu-shellcheck

More CLI & Terminal