Debian Codegraph @

cmd/shfmt/shfmt.1.scd

raw
1 shfmt(1)
2
3 ; To render and view: scdoc <shfmt.1.scd | man -l -
4
5 # NAME
6
7 shfmt - Format shell programs
8
9 # SYNOPSIS
10
11 *shfmt* [flags] [path...]
12
13 # DESCRIPTION
14
15 shfmt formats shell programs. If the only argument is a dash (*-*) or no
16 arguments are given, standard input will be used. If a given path is a
17 directory, all shell scripts found under that directory will be used.
18
19 If any EditorConfig files are found, they will be used to apply formatting
20 options. If any parser or printer flags are given to the tool, no EditorConfig
21 formatting options will be used. A default like *-i=0* can be used for this purpose.
22
23 shfmt's default shell formatting was chosen to be consistent, common, and
24 predictable. Some aspects of the format can be configured via printer flags.
25
26 # OPTIONS
27
28 ## Generic flags
29
30 *--version*
31 Show version and exit.
32
33 *-l[=0]*, *--list[=0]*
34 Error with a list of files whose formatting differs from shfmt;
35 paths are separated by a newline or a null character if -l=0
36
37 *-w*, *--write*
38 Write result to file instead of stdout.
39
40 *-d*, *--diff*
41 Error with a diff when the formatting differs.
42
43 The diff uses color when the output is a terminal.
44 To never use color, set a non-empty *NO_COLOR* or *TERM=dumb*.
45 To always use color, set a non-empty *FORCE_COLOR*.
46
47 *--apply-ignore*
48 Always apply EditorConfig ignore rules.
49
50 When formatting files directly, ignore rules are skipped without this flag.
51 Should be useful to any tools or editors which format stdin or a single file.
52 When printing results to stdout, an ignored file results in no output at all.
53
54 *--detect* <str>
55 How to detect shell files when walking directories or via *--find*:
56 *default*, *exec*, or *all*.
57
58 By default, files with a non-shell extension like *foo.go* are skipped;
59 only shell extensions like *foo.sh* and extensionless files with a shell
60 shebang are used. *exec* also reads the shebangs of executable files,
61 costing an extra stat per skipped file, and *all* reads the shebangs of
62 all regular files.
63
64 *--filename* str
65 Provide a name for the standard input file.
66
67 Use of this flag is necessary for EditorConfig support to work with stdin,
68 since EditorConfig files are found relative to the location of a script.
69
70 ## Parser flags
71
72 *-ln*, *--language-dialect* <str>
73 Language dialect (*bash*/*posix*/*mksh*/*bats*/*zsh*, default *auto*).
74
75 When set to *auto*, the language is detected from the input filename,
76 as long as it has a shell extension like *foo.mksh*. Otherwise, if the input
77 begins with a shell shebang like *#!/bin/sh*, that's used instead.
78 If neither come up with a result, *bash* is used as a fallback.
79
80 The filename extension *.sh* is a special case: it implies *posix*,
81 but may be overridden by a valid shell shebang.
82
83 *-p*, *--posix*
84 Shorthand for *-ln=posix*.
85
86 *-s*, *--simplify*
87 Simplify the code.
88
89 ## Printer flags
90
91 *-i*, *--indent* <uint>
92 Indent: *0* for tabs (default), *>0* for number of spaces.
93
94 *-bn*, *--binary-next-line*
95 Binary ops like *&&* and *|* may start a line.
96
97 *-ci*, *--case-indent*
98 Switch cases will be indented.
99
100 *-sr*, *--space-redirects*
101 Redirect operators will be followed by a space.
102
103 *-kp*, *--keep-padding*
104 Keep column alignment paddings.
105
106 This flag is *DEPRECATED* and will be removed in the next major version.
107 For more information, see: https://github.com/mvdan/sh/issues/658
108
109 *-fn*, *--func-next-line*
110 Function opening braces are placed on a separate line.
111
112 *-mn*, *--minify*
113 Minify the code to reduce its size (implies *-s*).
114
115 ## Utility flags
116
117 *-f[=0]*, *--find[=0]*
118 Recursively find all shell files and print the paths;
119 paths are separated by a newline or a null character if -f=0.
120
121 *--to-json*
122 Print syntax tree to stdout as a typed JSON.
123
124 *--from-json*
125 Read syntax tree from stdin as a typed JSON.
126
127 # EXAMPLES
128
129 Format all the scripts under the current directory, printing which are modified:
130
131 shfmt -l -w .
132
133 For CI, one can use a variant where formatting changes are just shown as diffs:
134
135 shfmt -d .
136
137 The following formatting flags closely resemble Google's shell style defined in
138 <https://google.github.io/styleguide/shellguide.html>:
139
140 shfmt -i 2 -ci -bn
141
142 Below is a sample EditorConfig file as defined by <https://editorconfig.org/>,
143 showing how to set supported options:
144
145 ```
146 [*.sh]
147 # like -i=4
148 indent_style = space
149 indent_size = 4
150
151 # --language-dialect
152 shell_variant = posix
153 simplify = true
154 binary_next_line = true
155 # --case-indent
156 switch_case_indent = true
157 space_redirects = true
158 keep_padding = true
159 # --func-next-line
160 function_next_line = true
161 minify = true
162
163 # Ignore the entire "third_party" directory when calling shfmt on directories,
164 # such as "shfmt -l -w .". When formatting files directly,
165 # like "shfmt -w third_party/foo.sh" or "shfmt --filename=third_party/foo.sh",
166 # the ignore logic is applied only when the --apply-ignore flag is given.
167 [third_party/**]
168 ignore = true
169 ```
170
171 EditorConfig sections may also use `[[shell]]`, `[[bash]]`, or `[[zsh]]` to match shell scripts,
172 which is particularly useful when scripts use a shebang but no extension.
173 Note that this feature is outside of the EditorConfig spec and may be changed in the future.
174
175 shfmt can also replace *bash -n* to check shell scripts for syntax errors. It is
176 more exhaustive, as it parses all syntax statically and requires valid UTF-8:
177
178 ```
179 $ echo '${foo:1 2}' | bash -n
180 $ echo '${foo:1 2}' | shfmt >/dev/null
181 1:9: not a valid arithmetic operator: 2
182 $ echo 'foo=(1 2)' | bash --posix -n
183 $ echo 'foo=(1 2)' | shfmt -p >/dev/null
184 1:5: arrays are a bash feature
185 ```
186
187 # AUTHORS
188
189 Maintained by Daniel Martí <[email protected]>, who is assisted by other open
190 source contributors. For more information and development, see
191 <https://github.com/mvdan/sh>.