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           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
44           Simplify the code.
45
46       -mn
47           Minify the code to reduce its size (implies -s).
48
49   Parser flags
50       -ln <str>
51           Language variant to parse (bash/posix/mksh/bats, default bash).
52
53       -p
54           Shorthand for -ln=posix.
55
56       -filename str
57           Provide a name for the standard input file.
58
59   Printer flags
60       -i <uint>
61           Indent: 0 for tabs (default), >0 for number of spaces.
62
63       -bn
64           Binary ops like && and | may start a line.
65
66       -ci
67           Switch cases will be indented.
68
69       -sr
70           Redirect operators will be followed by a space.
71
72       -kp
73           Keep column alignment paddings.
74
75       -fn
76           Function opening braces are placed on a separate line.
77
78   Utility flags
79       -f
80           Recursively find all shell files and print the paths.
81
82       -tojson
83           Print syntax tree to stdout as a typed JSON.
84

EXAMPLES

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

AUTHORS

132       Maintained by Daniel Martí <mvdan@mvdan.cc>, who is assisted by other
133       open source contributors. For more information and development, see
134       <https://github.com/mvdan/sh>.
135
136
137
138                                  2022-02-23                          shfmt(1)
Impressum