1shfmt(1) General Commands Manual shfmt(1)
2
3
4
6 shfmt - Format shell programs
7
9 shfmt [flags] [path...]
10
12 shfmt formats shell programs. If the only argument is a dash (-) or no
13 arguments are given, standard input will be used. If a given path is a
14 directory, all shell scripts found under that directory will be used.
15
16 If any EditorConfig files are found, they will be used to apply format‐
17 ting options. If any parser or printer flags are given to the tool, no
18 EditorConfig files will be used. A default like -i=0 can be used for
19 this purpose.
20
21 shfmt's default shell formatting was chosen to be consistent, common,
22 and predictable. Some aspects of the format can be configured via
23 printer flags.
24
26 Generic flags
27 -version
28 Show version and exit.
29
30 -l, --list
31 List files whose formatting differs from shfmt's.
32
33 -w, --write
34 Write result to file instead of stdout.
35
36 -d, --diff
37 Error with a diff when the formatting differs.
38
39 The diff uses color when the output is a terminal. To never use
40 color, set a non-empty NO_COLOR or TERM=dumb. To always use color,
41 set a non-empty FORCE_COLOR.
42
43 -s, --simplify
44 Simplify the code.
45
46 -mn, --minify
47 Minify the code to reduce its size (implies -s).
48
49 Parser flags
50 -ln, --language-dialect <str>
51 Language dialect (bash/posix/mksh/bats, default auto).
52
53 When set to auto, the language is detected from the input filename,
54 as long as it has a shell extension like foo.mksh. Otherwise, if
55 the input begins with a shell shebang like #!/bin/sh, that's used
56 instead. If neither come up with a result, bash is used as a fall‐
57 back.
58
59 The filename extension .sh is a special case: it implies posix, but
60 may be overriden by a valid shell shebang.
61
62 -p, --posix
63 Shorthand for -ln=posix.
64
65 -filename str
66 Provide a name for the standard input file.
67
68 Printer flags
69 -i, --indent <uint>
70 Indent: 0 for tabs (default), >0 for number of spaces.
71
72 -bn, --binary-next-line
73 Binary ops like && and | may start a line.
74
75 -ci, --switch-case-indent
76 Switch cases will be indented.
77
78 -sr, --space-redirects
79 Redirect operators will be followed by a space.
80
81 -kp, --keep-padding
82 Keep column alignment paddings.
83
84 -fn, --func-next-line
85 Function opening braces are placed on a separate line.
86
87 Utility flags
88 -f, --find
89 Recursively find all shell files and print the paths.
90
91 -tojson
92 Print syntax tree to stdout as a typed JSON.
93
95 Format all the scripts under the current directory, printing which are
96 modified:
97
98 shfmt -l -w .
99
100 For CI, one can use a variant where formatting changes are just shown
101 as diffs:
102
103 shfmt -d .
104
105 The following formatting flags closely resemble Google's shell style
106 defined in <https://google.github.io/styleguide/shell.xml>:
107
108 shfmt -i 2 -ci -bn
109
110 Below is a sample EditorConfig file as defined by <https://editorcon‐
111 fig.org/>, showing how to set any option:
112
113 [*.sh]
114 # like -i=4
115 indent_style = space
116 indent_size = 4
117
118 shell_variant = posix # --language-variant
119 binary_next_line = true
120 switch_case_indent = true # --case-indent
121 space_redirects = true
122 keep_padding = true
123 function_next_line = true # --func-next-line
124
125 # Ignore the entire "third_party" directory.
126 [third_party/**]
127 ignore = true
128
129 shfmt can also replace bash -n to check shell scripts for syntax er‐
130 rors. It is more exhaustive, as it parses all syntax statically and re‐
131 quires valid UTF-8:
132
133 $ echo '${foo:1 2}' | bash -n
134 $ echo '${foo:1 2}' | shfmt >/dev/null
135 1:9: not a valid arithmetic operator: 2
136 $ echo 'foo=(1 2)' | bash --posix -n
137 $ echo 'foo=(1 2)' | shfmt -p >/dev/null
138 1:5: arrays are a bash feature
139
141 Maintained by Daniel Martí <mvdan@mvdan.cc>, who is assisted by other
142 open source contributors. For more information and development, see
143 <https://github.com/mvdan/sh>.
144
145
146
147 2023-01-19 shfmt(1)