shfmt(1) | General Commands Manual | shfmt(1) |
shfmt - Format shell programs
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.
If any EditorConfig files are found, they will be used to apply formatting options. If any parser or printer flags are given to the tool, no EditorConfig formatting options will be used. A default like -i=0 can be used for this purpose.
shfmt's default shell formatting was chosen to be consistent, common, and predictable. Some aspects of the format can be configured via printer flags.
-version
-l, --list
-w, --write
-d, --diff
The diff uses color when the output is a terminal. To never use color, set a non-empty NO_COLOR or TERM=dumb. To always use color, set a non-empty FORCE_COLOR.
-s, --simplify
-mn, --minify
--apply-ignore
When formatting files directly, ignore rules are skipped without this flag. Should be useful to any tools or editors which format stdin or a single file. When printing results to stdout, an ignored file results in no output at all.
-ln, --language-dialect <str>
When set to auto, the language is detected from the input filename, as long as it has a shell extension like foo.mksh. Otherwise, if the input begins with a shell shebang like #!/bin/sh, that's used instead. If neither come up with a result, bash is used as a fallback.
The filename extension .sh is a special case: it implies posix, but may be overriden by a valid shell shebang.
-p, --posix
-filename str
-i, --indent <uint>
-bn, --binary-next-line
-ci, --case-indent
-sr, --space-redirects
-kp, --keep-padding
-fn, --func-next-line
-f, --find
--to-json
--from-json
Format all the scripts under the current directory, printing which are modified:
For CI, one can use a variant where formatting changes are just shown as diffs:
The following formatting flags closely resemble Google's shell style defined in <https://google.github.io/styleguide/shellguide.html>:
Below is a sample EditorConfig file as defined by <https://editorconfig.org/>, showing how to set any option:
[*.sh] # like -i=4 indent_style = space indent_size = 4 # --language-variant shell_variant = posix binary_next_line = true # --case-indent switch_case_indent = true space_redirects = true keep_padding = true # --func-next-line function_next_line = true # Ignore the entire "third_party" directory when calling shfmt on directories, # such as "shfmt -l -w .". When formatting files directly, # like "shfmt -w third_party/foo.sh" or "shfmt --filename=third_party/foo.sh", # the ignore logic is applied only when the --apply-ignore flag is given. [third_party/**] ignore = true
EditorConfig sections may also use `[[shell]]` or `[[bash]]` to match any shell or bash scripts, which is particularly useful when scripts use a shebang but no extension. Note that this feature is outside of the EditorConfig spec and may be changed in the future.
shfmt can also replace bash -n to check shell scripts for syntax errors. It is more exhaustive, as it parses all syntax statically and requires valid UTF-8:
$ echo '${foo:1 2}' | bash -n $ echo '${foo:1 2}' | shfmt >/dev/null 1:9: not a valid arithmetic operator: 2 $ echo 'foo=(1 2)' | bash --posix -n $ echo 'foo=(1 2)' | shfmt -p >/dev/null 1:5: arrays are a bash feature
Maintained by Daniel Martí <mvdan@mvdan.cc>, who is assisted by other open source contributors. For more information and development, see <https://github.com/mvdan/sh>.
2024-02-18 |