LinuxCommandLibrary

shfmt

Format shell scripts automatically

TLDR

Print a formatted version of a shell script

$ shfmt [path/to/file]
copy

List unformatted files
$ shfmt --list [path/to/directory]
copy

Write the result to the file instead of printing it to the terminal
$ shfmt --write [path/to/file]
copy

Simplify the code, removing redundant pieces of syntax (i.e. removing "$" from vars in expressions)
$ shfmt --simplify [path/to/file]
copy

SYNOPSIS

shfmt [options] [file ...]
shfmt [options] directory ...

PARAMETERS

-f
    Filter files to format by glob pattern.

-l
    List files whose formatting differs from shfmt's style.

-w
    Write result to source file instead of standard output.

-d
    Display diffs instead of writing results to files.

-p
    Parse and format the file as a Go template.

-i
    Set indentation style (number of spaces). Use `0` for tabs.

-bn
    Binary operators (like &&, ||) stay on their line.

-kp
    Keep parentheses on the same line as the statement.

-s
    Simplify the code (e.g., remove redundant $ for command substitutions).

-sr
    Keep spaces around [[ ]] expressions.

-ln
    Line number for error reporting.

-ci
    Indent switch cases.

-fn
    Force func keyword for function definitions.

-st
    Simplify test commands ([ ] to [[ ]]).

-o
    Write formatted output to file (only with one input file).

-v
    Print version and exit.

-h
    Show help and exit.

DESCRIPTION

shfmt is an opinionated shell script formatter for sh, bash, mksh, and dash. It aims to produce consistent and readable shell code by automatically adjusting indentation, spacing, and other stylistic elements. Inspired by gofmt, shfmt strives for a single, canonical format, eliminating debates over coding style. It's built on a robust parser, ensuring that the formatted output remains syntactically correct and semantically equivalent to the original script. This tool is invaluable for maintaining code quality, especially in collaborative environments, as it standardizes the appearance of shell scripts across projects and teams. It can format files in place, print to standard output, or show diffs.

CAVEATS

shfmt is opinionated and applies a fixed set of formatting rules. While it aims to be non-destructive, scripts with highly unusual or tricky syntax might behave unexpectedly, though this is rare. It also does not perform static analysis or linting; for that, tools like shellcheck are more appropriate. Users transitioning to shfmt might find the initial reformatting of existing codebases substantial.

<B>UNCOMPROMISING STYLE</B>

shfmt is designed to be an opinionated formatter. It dictates a single, consistent style for shell scripts, removing the need for manual style guides and endless debates over formatting preferences. This "uncompromising" approach ensures uniform code appearance across projects.

<B>GO IMPLEMENTATION</B>

The tool is implemented in Go, leveraging Go's robust parser infrastructure. This implementation choice contributes to shfmt's speed, reliability, and ease of distribution as a single static binary.

HISTORY

shfmt was created by Daniel Martí and first released in 2016. Its development was inspired by gofmt, the canonical formatter for the Go programming language, aiming to provide a similar "uncompromising" and consistent formatting experience for shell scripts. It quickly gained popularity within the Go community and beyond due to its reliability and the clarity it brings to shell scripting, addressing a long-standing need for a robust shell code formatter.

SEE ALSO

shellcheck(1), bash(1), sh(1), gofmt(1)

Copied to clipboard