Back to Skills

shfmt-formatting

verified

Use when formatting shell scripts with shfmt. Covers consistent formatting patterns, shell dialect support, common issues, and editor integration.

View on GitHub

Marketplace

han

TheBushidoCollective/han

Plugin

jutsu-shfmt

Technique

Repository

TheBushidoCollective/han
60stars

jutsu/jutsu-shfmt/skills/shfmt-formatting/SKILL.md

Last Verified

January 24, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-shfmt/skills/shfmt-formatting/SKILL.md -a claude-code --skill shfmt-formatting

Installation paths:

Claude
.claude/skills/shfmt-formatting/
Powered by add-skill CLI

Instructions

# shfmt Formatting

Expert knowledge of shfmt's formatting capabilities, patterns, and integration with development workflows for consistent shell script formatting.

## Overview

shfmt formats shell scripts for readability and consistency. It parses scripts into an AST and prints them in a canonical format, eliminating style debates and ensuring uniformity across codebases.

## Supported Shell Dialects

shfmt supports multiple shell dialects with different syntax rules:

### POSIX Shell

Most portable, works with `/bin/sh` on any Unix system:

```bash
#!/bin/sh
# POSIX-compliant syntax only

# No arrays
# No [[ ]] tests
# No $() must work in all contexts

if [ "$var" = "value" ]; then
    echo "match"
fi
```

### Bash

Most common for scripts, supports extended features:

```bash
#!/usr/bin/env bash
# Bash-specific features allowed

declare -a array=("one" "two" "three")

if [[ "$var" == "value" ]]; then
    echo "match"
fi

result=$((1 + 2))
```

### mksh (MirBSD Korn Shell)

Korn shell variant with its own extensions:

```bash
#!/bin/mksh
# mksh-specific syntax

typeset -A assoc
assoc[key]=value
```

### Bats (Bash Automated Testing System)

For Bats test files:

```bash
#!/usr/bin/env bats

@test "example test" {
    run my_command
    [ "$status" -eq 0 ]
}
```

## Formatting Patterns

### Indentation

shfmt normalizes indentation throughout scripts:

**Before:**

```bash
if [ "$x" = "y" ]; then
  echo "two spaces"
    echo "four spaces"
 echo "tab"
fi
```

**After (with `-i 2`):**

```bash
if [ "$x" = "y" ]; then
  echo "two spaces"
  echo "four spaces"
  echo "tab"
fi
```

### Spacing

shfmt normalizes spacing around operators and keywords:

**Before:**

```bash
if[$x="y"];then
echo "no spaces"
fi
x=1;y=2;z=3
```

**After:**

```bash
if [ $x = "y" ]; then
    echo "no spaces"
fi
x=1
y=2
z=3
```

### Semicolons and Newlines

Multiple statements are split to separate lines:

**Before:**

```bash
if [ "$x" ]; then echo "yes"; else echo "no"; fi
```

**After:**

`

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
8462 chars