Bash

๐Ÿงช ShellCheck + GitHub Actions (CI for Bash)

ShellCheck is a static analysis tool for shell scripts. It warns you about syntax errors, bad practices, and potential bugs.

This guide shows how to:


๐Ÿ› ๏ธ What is ShellCheck?

ShellCheck analyzes Bash scripts and reports:


๐Ÿ”ง 1. Installing ShellCheck

โœ… Ubuntu / Debian

sudo apt install shellcheck

โœ… macOS (Homebrew)

brew install shellcheck

โœ… Manual (Portable)

Download from: https://github.com/koalaman/shellcheck/releases


๐Ÿงช 2. Using ShellCheck Locally

Run it on a script:

shellcheck script.sh

To check all scripts in your repo:

find . -name "*.sh" -exec shellcheck {} \;

๐Ÿš€ 3. GitHub Actions: shellcheck.yml

โž• Add Workflow File

Create .github/workflows/shellcheck.yml:

name: ShellCheck Linter

on:
  push:
    paths:
      - '**.sh'
  pull_request:
    paths:
      - '**.sh'

jobs:
  lint:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Install ShellCheck
      run: sudo apt-get install -y shellcheck

    - name: Run ShellCheck
      run: |
        find . -name "*.sh" | xargs shellcheck

โœ… Features:


๐Ÿง  Example Output

In script.sh line 4:
for f in $(ls *.txt); do
         ^-- SC2045: Iterating over ls output is fragile. Use globs.

๐Ÿงน Best Practices

Practice Why
Fix all shellcheck warnings Improve reliability
Run on every PR Maintain quality
Use consistent quoting Prevent bugs
Enable set -euo pipefail in scripts Add extra safety

๐Ÿ“š References


๐Ÿ“ File: advance/shellcheck-ci.md
Author: Kashif Alam