Get the FREE Ultimate OpenClaw Setup Guide →

shell-script-quality

npx machina-cli add skill d-oit/gemini-search-plugin/shell-script-quality --openclaw
Files (1)
SKILL.md
5.0 KB

Shell Script Quality

Comprehensive shell script linting and testing using ShellCheck and BATS with 2025 best practices.

Quick Start

Copy this workflow checklist and track your progress:

Shell Script Quality Workflow:
- [ ] Step 1: Lint with ShellCheck
- [ ] Step 2: Fix reported issues
- [ ] Step 3: Write BATS tests
- [ ] Step 4: Verify tests pass
- [ ] Step 5: Integrate into CI/CD

Core Workflow

Step 1: Lint with ShellCheck

# Lint single file
shellcheck script.sh

# Lint all scripts
find scripts -name "*.sh" -exec shellcheck {} +

# Use config file if present
shellcheck -x script.sh

Common fixes: See SHELLCHECK.md for fix patterns

Step 2: Fix Reported Issues

Apply fixes for common warnings:

  • SC2086: Quote variables: "$var" not $var
  • SC2155: Separate declaration and assignment
  • SC2181: Check exit code directly with if ! command

For detailed fixes: See SHELLCHECK.md

Step 3: Write BATS Tests

#!/usr/bin/env bats

setup() {
    source "$BATS_TEST_DIRNAME/../scripts/example.sh"
}

@test "function succeeds with valid input" {
    run example_function "test"
    [ "$status" -eq 0 ]
    [ -n "$output" ]
}

@test "function fails with invalid input" {
    run example_function ""
    [ "$status" -ne 0 ]
    [[ "$output" =~ "ERROR" ]]
}

Test patterns: See BATS.md for comprehensive testing guide

Step 4: Run Tests

# Run all tests
bats tests/

# Run with verbose output
bats -t tests/

# Run specific file
bats tests/example.bats

If tests fail: Review error output, fix issues, re-run validation

Step 5: CI/CD Integration

GitHub Actions: See CI-CD.md for complete workflows

Quick integration:

- name: ShellCheck
  uses: ludeeus/action-shellcheck@master
- name: Run BATS
  run: |
    sudo apt-get install -y bats
    bats tests/

Script Template

Use this template for new scripts:

#!/bin/bash
set -euo pipefail

# Script directory (portable)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Error handler
error_exit() {
    echo "ERROR: $1" >&2
    exit "${2:-1}"
}

# Main function
main() {
    [[ $# -lt 1 ]] && {
        echo "Usage: $0 <argument>" >&2
        exit 1
    }

    # Your logic here
}

# Run if executed directly
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    main "$@"
fi

Installation

ShellCheck:

brew install shellcheck         # macOS
sudo apt-get install shellcheck # Linux

BATS:

brew install bats-core          # macOS
sudo apt-get install bats       # Linux

Configuration

.shellcheckrc in project root:

shell=bash
disable=SC1090
enable=all
source-path=SCRIPTDIR

For configuration details: See CONFIG.md

Testing Claude Code Plugins

Test scripts using CLAUDE_PLUGIN_ROOT:

@test "plugin script works" {
    export CLAUDE_PLUGIN_ROOT="$BATS_TEST_DIRNAME/.."
    run bash "$CLAUDE_PLUGIN_ROOT/scripts/search.sh" "query"
    [ "$status" -eq 0 ]
}

Test hooks with JSON:

@test "hook provides suggestions" {
    local input='{"tool":"Edit","params":{"file_path":"test.txt"}}'
    run bash "$HOOK_DIR/pre-edit.sh" <<< "$input"
    [ "$status" -eq 0 ]
    echo "$output" | jq empty
}

More plugin patterns: See PATTERNS.md

Troubleshooting

ShellCheck:

  • SC1090 warnings: Add # shellcheck source=path/to/file.sh
  • False positives: Use # shellcheck disable=SCxxxx

BATS:

  • Tests interfere: Ensure proper teardown() cleanup
  • Can't source script: Add main execution guard
  • Path issues: Use $BATS_TEST_DIRNAME for relative paths

Detailed troubleshooting: See TROUBLESHOOTING.md

Validation Loop Pattern

For quality-critical operations:

  1. Make changes to script
  2. Validate immediately: shellcheck script.sh
  3. If validation fails:
    • Review error messages carefully
    • Fix the issues
    • Run validation again
  4. Only proceed when validation passes
  5. Run tests: bats tests/script.bats
  6. If tests fail, return to step 1

Reference Files

Quick Quality Check

Run this command for complete validation:

# Check everything
find scripts -name "*.sh" -exec shellcheck {} + && bats tests/

# Or use quality check script
bash scripts/check-quality.sh

Source

git clone https://github.com/d-oit/gemini-search-plugin/blob/main/.claude/skills/shell-script-quality/SKILL.mdView on GitHub

Overview

Shell Script Quality provides comprehensive linting and testing for bash/sh scripts using ShellCheck and BATS, aligned with 2025 best practices. It guides you from linting to CI/CD integration to improve Bash code quality.

How This Skill Works

The workflow lints scripts with ShellCheck (optionally using a config file and -x), then helps you fix common warnings such as SC2086, SC2155, and SC2181. After linting, you write BATS tests to cover expected behavior, run them locally, and finally integrate the test suite into your CI/CD pipeline.

When to Use It

  • When auditing existing bash/sh scripts for syntax and quality issues
  • When writing unit tests for shell scripts using BATS
  • When addressing ShellCheck warnings such as SC2086, SC2155, or SC2181
  • When setting up CI/CD to automatically lint and test shell scripts on push or PR
  • When aiming to improve overall bash code quality and reliability across projects

Quick Start

  1. Step 1: Lint with ShellCheck
  2. Step 2: Write BATS tests
  3. Step 3: Integrate into CI/CD and verify locally

Best Practices

  • Run ShellCheck regularly and fix issues before merging
  • Quote all variables: "$var" to avoid word splitting
  • Prefer separate declarations and assignments (SC2155) for clarity
  • Check command exit codes directly (SC2181) and fail fast
  • Include a main guard and proper execution checks to prevent sourcing issues

Example Use Cases

  • Lint a single file: shellcheck script.sh
  • Lint all scripts: find scripts -name *.sh -exec shellcheck {} +
  • Write a BATS test to verify a function's behavior
  • Run tests: bats tests/ or bats tests/example.bats
  • Integrate ShellCheck and BATS into a GitHub Actions workflow

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers