shfmt(1)
π shfmt(1) β General Commands Manual, but make it cute
okay so bestie, welcome to the shfmt(1) page from the General Commands Manual β this is literally the glow-up station for your shell scripts and i'm obsessed π
β¨ NAME β¨
shfmt β Format shell programs π
β¨ SYNOPSIS β¨
shfmt [flags] [path...]
β¨ DESCRIPTION β¨
shfmt formats shell programs, periodt. π«Ά If the only argument is a dash (-) or you give it no arguments at all, standard input gets used. And if a given path is a directory, it takes all the shell scripts found under that directory. slay.
If any EditorConfig files are found, they will be used to apply formatting options. But heads up: if any parser or printer flags are given to the tool, then no EditorConfig formatting options will be used β like, none, zero, the flags win π
. You can use a default like -i=0 for this purpose if you just want to shut EditorConfig out.
shfmt's default shell formatting was chosen to be consistent, common, and predictable. Some aspects of the format can be configured via printer flags. it's giving thoughtful π
β¨ OPTIONS β¨
π¦ Generic flags
--versionβ Show version and exit. bye bestie π-l[=0],--list[=0]β Error with a list of files whose formatting differs fromshfmt; paths are separated by a newline or a null character if-l=0.-w,--writeβ Write result to file instead of stdout. the real glow-up applied directly to your files π-d,--diffβ Error with a diff when the formatting differs.
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.
--apply-ignoreβ Always apply EditorConfig ignore rules.
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. not even a crumb π
--detect <str>β How to detect shell files when walking directories or via--find:default,exec, orall.
By default, files with a non-shell extension like foo.go are skipped; only shell extensions like foo.sh and extensionless files with a shell shebang are used. exec also reads the shebangs of executable files, costing an extra stat per skipped file, and all reads the shebangs of all regular files. ngl that all option is doing the most π₯
--filename strβ Provide a name for the standard input file.
Use of this flag is necessary for EditorConfig support to work with stdin, since EditorConfig files are found relative to the location of a script. so if stdin's acting mysterious, this is why π
πΈ Parser flags
-ln,--language-dialect <str>β Language dialect (bash/posix/mksh/bats/zsh, defaultauto).
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. a queen always has a backup plan π
The filename extension .sh is a special case: it implies posix, but may be overridden by a valid shell shebang. iconique β¨
-p,--posixβ Shorthand for-ln=posix.-s,--simplifyβ Simplify the code. less chaos, more slay.
π Printer flags
-i,--indent <uint>β Indent:0for tabs (default),>0for number of spaces. tabs girlies rise π-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.
π¨ This flag is DEPRECATED and will be removed in the next major version. For more information, see: https://github.com/mvdan/sh/issues/658 β like, enjoy it while it lasts bestie π
-fn,--func-next-lineβ Function opening braces are placed on a separate line.-mn,--minifyβ Minify the code to reduce its size (implies-s). shrinking your script like it's a crop top π
π§ Utility flags
-f[=0],--find[=0]β Recursively find all shell files and print the paths; paths are separated by a newline or a null character if-f=0.--to-jsonβ Print syntax tree to stdout as a typed JSON.--from-jsonβ Read syntax tree from stdin as a typed JSON. the round trip is real π
β¨ EXAMPLES β¨
Format all the scripts under the current directory, printing which are modified:
shfmt -l -w .
For CI, one can use a variant where formatting changes are just shown as diffs:
shfmt -d .
The following formatting flags closely resemble Google's shell style defined in https://google.github.io/styleguide/shellguide.html:
shfmt -i 2 -ci -bn
Below is a sample EditorConfig file as defined by https://editorconfig.org/, showing how to set supported options:
[*.sh]
# like -i=4
indent_style = space
indent_size = 4
# --language-dialect
shell_variant = posix
simplify = true
binary_next_line = true
# --case-indent
switch_case_indent = true
space_redirects = true
keep_padding = true
# --func-next-line
function_next_line = true
minify = 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]], [[bash]], or [[zsh]] to match shell 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. so don't get too attached, k? π
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
see how it caught stuff bash just let slide?? main character energy π
β¨ AUTHORS β¨
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. π«Ά
and that's the whole page, bestie π This little gem is shfmt(1) from the General Commands Manual, dated 2026-09-07 β go forth and format, you gorgeous shell gremlin β¨ππ¦
π¦ where this page comes from
- arch rolling: shfmt 3.14.1-1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms