.\" Generated by scdoc 1.11.4 .\" Complete documentation for this program is not available as a GNU info page .ie \n(.g .ds Aq \(aq .el .ds Aq ' .nh .ad l .\" Begin generated content: .TH "shfmt" "1" "2026-04-07" .PP .PP .SH NAME .PP shfmt - Format shell programs .PP .SH SYNOPSIS .PP \fBshfmt\fR [flags] [path.\&.\&.\&] .PP .SH DESCRIPTION .PP shfmt formats shell programs.\& If the only argument is a dash (\fB-\fR) 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.\& .PP 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 \fB-i=0\fR can be used for this purpose.\& .PP shfmt'\&s default shell formatting was chosen to be consistent, common, and predictable.\& Some aspects of the format can be configured via printer flags.\& .PP .SH OPTIONS .PP .SS Generic flags .PP \fB--version\fR .RS 4 Show version and exit.\& .PP .RE \fB-l[=0]\fR, \fB--list[=0]\fR .RS 4 Error with a list of files whose formatting differs from shfmt; paths are separated by a newline or a null character if -l=0 .PP .RE \fB-w\fR, \fB--write\fR .RS 4 Write result to file instead of stdout.\& .PP .RE \fB-d\fR, \fB--diff\fR .RS 4 Error with a diff when the formatting differs.\& .PP The diff uses color when the output is a terminal.\& To never use color, set a non-empty \fBNO_COLOR\fR or \fBTERM=dumb\fR.\& To always use color, set a non-empty \fBFORCE_COLOR\fR.\& .PP .RE \fB--apply-ignore\fR .RS 4 Always apply EditorConfig ignore rules.\& .PP 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.\& .PP .RE \fB--filename\fR str .RS 4 Provide a name for the standard input file.\& .PP 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.\& .PP .RE .SS Parser flags .PP \fB-ln\fR, \fB--language-dialect\fR .RS 4 Language dialect (\fBbash\fR/\fBposix\fR/\fBmksh\fR/\fBbats\fR/\fBzsh\fR, default \fBauto\fR).\& .PP When set to \fBauto\fR, the language is detected from the input filename, as long as it has a shell extension like \fBfoo.\&mksh\fR.\& Otherwise, if the input begins with a shell shebang like \fB#!\&/bin/sh\fR, that'\&s used instead.\& If neither come up with a result, \fBbash\fR is used as a fallback.\& .PP The filename extension \fB.\&sh\fR is a special case: it implies \fBposix\fR, but may be overridden by a valid shell shebang.\& .PP .RE \fB-p\fR, \fB--posix\fR .RS 4 Shorthand for \fB-ln=posix\fR.\& .PP .RE \fB-s\fR, \fB--simplify\fR .RS 4 Simplify the code.\& .PP .RE .SS Printer flags .PP \fB-i\fR, \fB--indent\fR .RS 4 Indent: \fB0\fR for tabs (default), \fB>0\fR for number of spaces.\& .PP .RE \fB-bn\fR, \fB--binary-next-line\fR .RS 4 Binary ops like \fB&&\fR and \fB|\fR may start a line.\& .PP .RE \fB-ci\fR, \fB--case-indent\fR .RS 4 Switch cases will be indented.\& .PP .RE \fB-sr\fR, \fB--space-redirects\fR .RS 4 Redirect operators will be followed by a space.\& .PP .RE \fB-kp\fR, \fB--keep-padding\fR .RS 4 Keep column alignment paddings.\& .PP This flag is \fBDEPRECATED\fR and will be removed in the next major version.\& For more information, see: https://github.\&com/mvdan/sh/issues/658 .PP .RE \fB-fn\fR, \fB--func-next-line\fR .RS 4 Function opening braces are placed on a separate line.\& .PP .RE \fB-mn\fR, \fB--minify\fR .RS 4 Minify the code to reduce its size (implies \fB-s\fR).\& .PP .RE .SS Utility flags .PP \fB-f[=0]\fR, \fB--find[=0]\fR .RS 4 Recursively find all shell files and print the paths; paths are separated by a newline or a null character if -f=0.\& .PP .RE \fB--to-json\fR .RS 4 Print syntax tree to stdout as a typed JSON.\& .PP .RE \fB--from-json\fR .RS 4 Read syntax tree from stdin as a typed JSON.\& .PP .RE .SH EXAMPLES .PP Format all the scripts under the current directory, printing which are modified: .PP .RS 4 shfmt -l -w .\& .PP .RE For CI, one can use a variant where formatting changes are just shown as diffs: .PP .RS 4 shfmt -d .\& .PP .RE The following formatting flags closely resemble Google'\&s shell style defined in : .PP .RS 4 shfmt -i 2 -ci -bn .PP .RE Below is a sample EditorConfig file as defined by , showing how to set supported options: .PP .nf .RS 4 [*\&.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 .fi .RE .PP 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.\& .PP shfmt can also replace \fBbash -n\fR to check shell scripts for syntax errors.\& It is more exhaustive, as it parses all syntax statically and requires valid UTF-8: .PP .nf .RS 4 $ 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 .fi .RE .PP .SH AUTHORS .PP Maintained by Daniel Martí , who is assisted by other open source contributors.\& For more information and development, see .\&