Skip to content

shfmt

shfmt documentation

sh - GitHub

Configuration in MegaLinter

Variable Description Default value
BASH_SHFMT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
BASH_SHFMT_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
Include every file
BASH_SHFMT_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
Exclude no file
BASH_SHFMT_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
list_of_files
BASH_SHFMT_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".sh", ".bash", ".dash", ".ksh"]
BASH_SHFMT_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
BASH_SHFMT_PRE_COMMANDS List of bash commands to run before the linter None
BASH_SHFMT_POST_COMMANDS List of bash commands to run after the linter None
BASH_SHFMT_DISABLE_ERRORS Run linter but consider errors as warnings true
BASH_SHFMT_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

IDE Integration

Use shfmt in your favorite IDE to catch errors before MegaLinter !

IDE Extension Name Install
Atom format-shell Visit Web Site
Emacs emacs-shfmt Visit Web Site
IDEA shell-scripts Visit Web Site
micro micro Visit Web Site
Sublime Text Sublime-Pretty-Shell Visit Web Site
vim vim-shfmt Visit Web Site
Visual Studio Code shell-format Install in VsCode

MegaLinter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 97 Docker Image Size (tag) Docker Pulls
ci_light Optimized for CI items (Dockerfile, Jenkinsfile, JSON/YAML schemas,XML 16 Docker Image Size (tag) Docker Pulls
dart Optimized for DART based projects 42 Docker Image Size (tag) Docker Pulls
documentation MegaLinter for documentation projects 41 Docker Image Size (tag) Docker Pulls
dotnet Optimized for C, C++, C# or VB based projects 49 Docker Image Size (tag) Docker Pulls
go Optimized for GO based projects 43 Docker Image Size (tag) Docker Pulls
java Optimized for JAVA based projects 43 Docker Image Size (tag) Docker Pulls
javascript Optimized for JAVASCRIPT or TYPESCRIPT based projects 50 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 46 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 49 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 42 Docker Image Size (tag) Docker Pulls
rust Optimized for RUST based projects 42 Docker Image Size (tag) Docker Pulls
salesforce Optimized for Salesforce based projects 44 Docker Image Size (tag) Docker Pulls
scala Optimized for SCALA based projects 42 Docker Image Size (tag) Docker Pulls
swift Optimized for SWIFT based projects 42 Docker Image Size (tag) Docker Pulls
terraform Optimized for TERRAFORM based projects 47 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .sh, .bash, .dash, .ksh

How the linting is performed

  • shfmt is called once with the list of files as arguments

Example calls

shfmt -d myfile.sh
shfmt -w myfile.sh

Help content

usage: shfmt [flags] [path ...]

shfmt formats shell programs. If the only argument is a dash ('-') or no
arguments are given, standard input will be used. If a given path is a
directory, all shell scripts found under that directory will be used.

  --version  show version and exit

  -l,  --list      list files whose formatting differs from shfmt's
  -w,  --write     write result to file instead of stdout
  -d,  --diff      error with a diff when the formatting differs
  -s,  --simplify  simplify the code
  -mn, --minify    minify the code to reduce its size (implies -s)

Parser options:

  -ln, --language-dialect str  bash/posix/mksh/bats, default "auto"
  -p,  --posix                 shorthand for -ln=posix
  --filename str               provide a name for the standard input file

Printer options:

  -i,  --indent uint       0 for tabs (default), >0 for number of spaces
  -bn, --binary-next-line  binary ops like && and | may start a line
  -ci, --case-indent       switch cases will be indented
  -sr, --space-redirects   redirect operators will be followed by a space
  -kp, --keep-padding      keep column alignment paddings
  -fn, --func-next-line    function opening braces are placed on a separate line

Utilities:

  -f, --find   recursively find all shell files and print the paths
  --tojson     print syntax tree to stdout as a typed JSON

For more information, see 'man shfmt' and https://github.com/mvdan/sh.

Installation on mega-linter Docker image

  • Dockerfile commands :
FROM mvdan/shfmt:latest-alpine as shfmt
COPY --from=shfmt /bin/shfmt /usr/bin/

Example success log

Results of shfmt linter (version 3.2.1)
See documentation on https://megalinter.github.io/descriptors/bash_shfmt/
-----------------------------------------------

[SUCCESS] .automation/test/shell_shfmt/shell-shfmt_good_1.sh


Example error log

Results of shfmt linter (version 3.2.1)
See documentation on https://megalinter.github.io/descriptors/bash_shfmt/
-----------------------------------------------

[ERROR] .automation/test/shell_shfmt/shell-shfmt_bad_1.sh
    --- .automation/test/shell_shfmt/shell-shfmt_bad_1.sh.orig
    +++ .automation/test/shell_shfmt/shell-shfmt_bad_1.sh
    @@ -8,10 +8,10 @@

     # Check the shell
     if [ ${ERROR_CODE} -ne 0 ]; then
    -  echo "We did it!"
    -  exit 0
    +    echo "We did it!"
    +    exit 0
     else
    -  echo "We done goofed it..."
    -  echo "${HELLO_WORLD}"
    -  exit 1
    +    echo "We done goofed it..."
    +    echo "${HELLO_WORLD}"
    +    exit 1
     fi