1shfmt(1)                    General Commands Manual                   shfmt(1)
2
3
4

NAME

6       shfmt - Format shell programs
7

SYNOPSIS

9       shfmt [flags] [path...]
10

DESCRIPTION

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

OPTIONS

26   Generic flags
27       -version
28           Show version and exit.
29
30       -l
31           List files whose formatting differs from shfmt's.
32
33       -w
34           Write result to file instead of stdout.
35
36       -d
37           Error with a diff when the formatting differs.
38
39       -s
40           Simplify the code.
41
42       -mn
43           Minify the code to reduce its size (implies -s).
44
45   Parser flags
46       -ln <str>
47           Language variant to parse (bash/posix/mksh/bats, default "bash").
48
49       -p
50           Shorthand for -ln=posix.
51
52       -filename str
53           Provide a name for the standard input file.
54
55   Printer flags
56       -i <uint>
57           Indent: 0 for tabs (default), >0 for number of spaces.
58
59       -bn
60           Binary ops like && and | may start a line.
61
62       -ci
63           Switch cases will be indented.
64
65       -sr
66           Redirect operators will be followed by a space.
67
68       -kp
69           Keep column alignment paddings.
70
71       -fn
72           Function opening braces are placed on a separate line.
73
74   Utility flags
75       -f
76           Recursively find all shell files and print the paths.
77
78       -tojson
79           Print syntax tree to stdout as a typed JSON.
80

EXAMPLES

82       Format all the scripts under the current directory, printing which are
83       modified:
84
85           shfmt -l -w .
86
87       For CI, one can use a variant where formatting changes are just shown
88       as diffs:
89
90           shfmt -d .
91
92       The following formatting flags closely resemble Google's shell style
93       defined in <https://google.github.io/styleguide/shell.xml>:
94
95           shfmt -i 2 -ci
96
97       Below is a sample EditorConfig file as defined by <https://editorcon
98       fig.org/>, showing how to set any option:
99
100           [*.sh]
101           # like -i=4
102           indent_style = space
103           indent_size = 4
104
105           shell_variant      = posix # like -ln=posix
106           binary_next_line   = true  # like -bn
107           switch_case_indent = true  # like -ci
108           space_redirects    = true  # like -sr
109           keep_padding       = true  # like -kp
110           function_next_line = true  # like -fn
111
112           # Ignore the entire "third_party" directory.
113           [third_party/**]
114           ignore = true
115
116       shfmt can also replace bash -n to check shell scripts for syntax er‐
117       rors. It is more exhaustive, as it parses all syntax statically and re‐
118       quires valid UTF-8:
119
120           $ echo '${foo:1 2}' | bash -n
121           $ echo '${foo:1 2}' | shfmt >/dev/null
122           1:9: not a valid arithmetic operator: 2
123           $ echo 'foo=(1 2)' | bash --posix -n
124           $ echo 'foo=(1 2)' | shfmt -p >/dev/null
125           1:5: arrays are a bash feature
126

AUTHORS

128       Maintained by Daniel Martí <mvdan@mvdan.cc>, who is assisted by other
129       open source contributors. For more information and development, see
130       <https://github.com/mvdan/sh>.
131
132
133
134                                  2021-04-07                          shfmt(1)
Impressum