shellcheck.1 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. .\" Automatically generated by Pandoc 2.9.2.1
  2. .\"
  3. .TH "SHELLCHECK" "1" "" "Shell script analysis tool" ""
  4. .hy
  5. .SH NAME
  6. .PP
  7. shellcheck - Shell script analysis tool
  8. .SH SYNOPSIS
  9. .PP
  10. \f[B]shellcheck\f[R] [\f[I]OPTIONS\f[R]...] \f[I]FILES\f[R]...
  11. .SH DESCRIPTION
  12. .PP
  13. ShellCheck is a static analysis and linting tool for sh/bash scripts.
  14. It\[aq]s mainly focused on handling typical beginner and intermediate
  15. level syntax errors and pitfalls where the shell just gives a cryptic
  16. error message or strange behavior, but it also reports on a few more
  17. advanced issues where corner cases can cause delayed failures.
  18. .PP
  19. ShellCheck gives shell specific advice.
  20. Consider this line:
  21. .IP
  22. .nf
  23. \f[C]
  24. (( area = 3.14*r*r ))
  25. \f[R]
  26. .fi
  27. .IP \[bu] 2
  28. For scripts starting with \f[C]#!/bin/sh\f[R] (or when using
  29. \f[C]-s sh\f[R]), ShellCheck will warn that \f[C](( .. ))\f[R] is not
  30. POSIX compliant (similar to checkbashisms).
  31. .IP \[bu] 2
  32. For scripts starting with \f[C]#!/bin/bash\f[R] (or using
  33. \f[C]-s bash\f[R]), ShellCheck will warn that decimals are not
  34. supported.
  35. .IP \[bu] 2
  36. For scripts starting with \f[C]#!/bin/ksh\f[R] (or using
  37. \f[C]-s ksh\f[R]), ShellCheck will not warn at all, as \f[C]ksh\f[R]
  38. supports decimals in arithmetic contexts.
  39. .SH OPTIONS
  40. .TP
  41. \f[B]-a\f[R],\ \f[B]--check-sourced\f[R]
  42. Emit warnings in sourced files.
  43. Normally, \f[C]shellcheck\f[R] will only warn about issues in the
  44. specified files.
  45. With this option, any issues in sourced files will also be reported.
  46. .TP
  47. \f[B]-C\f[R][\f[I]WHEN\f[R]],\ \f[B]--color\f[R][=\f[I]WHEN\f[R]]
  48. For TTY output, enable colors \f[I]always\f[R], \f[I]never\f[R] or
  49. \f[I]auto\f[R].
  50. The default is \f[I]auto\f[R].
  51. \f[B]--color\f[R] without an argument is equivalent to
  52. \f[B]--color=always\f[R].
  53. .TP
  54. \f[B]-i\f[R]\ \f[I]CODE1\f[R][,\f[I]CODE2\f[R]...],\ \f[B]--include=\f[R]\f[I]CODE1\f[R][,\f[I]CODE2\f[R]...]
  55. Explicitly include only the specified codes in the report.
  56. Subsequent \f[B]-i\f[R] options are cumulative, but all the codes can be
  57. specified at once, comma-separated as a single argument.
  58. Include options override any provided exclude options.
  59. .TP
  60. \f[B]-e\f[R]\ \f[I]CODE1\f[R][,\f[I]CODE2\f[R]...],\ \f[B]--exclude=\f[R]\f[I]CODE1\f[R][,\f[I]CODE2\f[R]...]
  61. Explicitly exclude the specified codes from the report.
  62. Subsequent \f[B]-e\f[R] options are cumulative, but all the codes can be
  63. specified at once, comma-separated as a single argument.
  64. .TP
  65. \f[B]-f\f[R] \f[I]FORMAT\f[R], \f[B]--format=\f[R]\f[I]FORMAT\f[R]
  66. Specify the output format of shellcheck, which prints its results in the
  67. standard output.
  68. Subsequent \f[B]-f\f[R] options are ignored, see \f[B]FORMATS\f[R] below
  69. for more information.
  70. .TP
  71. \f[B]--list-optional\f[R]
  72. Output a list of known optional checks.
  73. These can be enabled with \f[B]-o\f[R] flags or \f[B]enable\f[R]
  74. directives.
  75. .TP
  76. \f[B]--norc\f[R]
  77. Don\[aq]t try to look for .shellcheckrc configuration files.
  78. .TP
  79. \f[B]-o\f[R]\ \f[I]NAME1\f[R][,\f[I]NAME2\f[R]...],\ \f[B]--enable=\f[R]\f[I]NAME1\f[R][,\f[I]NAME2\f[R]...]
  80. Enable optional checks.
  81. The special name \f[I]all\f[R] enables all of them.
  82. Subsequent \f[B]-o\f[R] options accumulate.
  83. This is equivalent to specifying \f[B]enable\f[R] directives.
  84. .TP
  85. \f[B]-P\f[R]\ \f[I]SOURCEPATH\f[R],\ \f[B]--source-path=\f[R]\f[I]SOURCEPATH\f[R]
  86. Specify paths to search for sourced files, separated by \f[C]:\f[R] on
  87. Unix and \f[C];\f[R] on Windows.
  88. This is equivalent to specifying \f[C]search-path\f[R] directives.
  89. .TP
  90. \f[B]-s\f[R]\ \f[I]shell\f[R],\ \f[B]--shell=\f[R]\f[I]shell\f[R]
  91. Specify Bourne shell dialect.
  92. Valid values are \f[I]sh\f[R], \f[I]bash\f[R], \f[I]dash\f[R] and
  93. \f[I]ksh\f[R].
  94. The default is to deduce the shell from the file\[aq]s \f[C]shell\f[R]
  95. directive, shebang, or \f[C].bash/.bats/.dash/.ksh\f[R] extension, in
  96. that order.
  97. \f[I]sh\f[R] refers to POSIX \f[C]sh\f[R] (not the system\[aq]s), and
  98. will warn of portability issues.
  99. .TP
  100. \f[B]-S\f[R]\ \f[I]SEVERITY\f[R],\ \f[B]--severity=\f[R]\f[I]severity\f[R]
  101. Specify minimum severity of errors to consider.
  102. Valid values in order of severity are \f[I]error\f[R],
  103. \f[I]warning\f[R], \f[I]info\f[R] and \f[I]style\f[R].
  104. The default is \f[I]style\f[R].
  105. .TP
  106. \f[B]-V\f[R],\ \f[B]--version\f[R]
  107. Print version information and exit.
  108. .TP
  109. \f[B]-W\f[R] \f[I]NUM\f[R],\ \f[B]--wiki-link-count=NUM\f[R]
  110. For TTY output, show \f[I]NUM\f[R] wiki links to more information about
  111. mentioned warnings.
  112. Set to 0 to disable them entirely.
  113. .TP
  114. \f[B]-x\f[R],\ \f[B]--external-sources\f[R]
  115. Follow \f[C]source\f[R] statements even when the file is not specified
  116. as input.
  117. By default, \f[C]shellcheck\f[R] will only follow files specified on the
  118. command line (plus \f[C]/dev/null\f[R]).
  119. This option allows following any file the script may \f[C]source\f[R].
  120. .RS
  121. .PP
  122. This option may also be enabled using \f[C]external-sources=true\f[R] in
  123. \f[C].shellcheckrc\f[R].
  124. This flag takes precedence.
  125. .RE
  126. .TP
  127. \f[B]FILES...\f[R]
  128. One or more script files to check, or \[dq]-\[dq] for standard input.
  129. .SH FORMATS
  130. .TP
  131. \f[B]tty\f[R]
  132. Plain text, human readable output.
  133. This is the default.
  134. .TP
  135. \f[B]gcc\f[R]
  136. GCC compatible output.
  137. Useful for editors that support compiling and showing syntax errors.
  138. .RS
  139. .PP
  140. For example, in Vim,
  141. \f[C]:set makeprg=shellcheck\[rs] -f\[rs] gcc\[rs] %\f[R] will allow
  142. using \f[C]:make\f[R] to check the script, and \f[C]:cnext\f[R] to jump
  143. to the next error.
  144. .IP
  145. .nf
  146. \f[C]
  147. <file>:<line>:<column>: <type>: <message>
  148. \f[R]
  149. .fi
  150. .RE
  151. .TP
  152. \f[B]checkstyle\f[R]
  153. Checkstyle compatible XML output.
  154. Supported directly or through plugins by many IDEs and build monitoring
  155. systems.
  156. .RS
  157. .IP
  158. .nf
  159. \f[C]
  160. <?xml version=\[aq]1.0\[aq] encoding=\[aq]UTF-8\[aq]?>
  161. <checkstyle version=\[aq]4.3\[aq]>
  162. <file name=\[aq]file\[aq]>
  163. <error
  164. line=\[aq]line\[aq]
  165. column=\[aq]column\[aq]
  166. severity=\[aq]severity\[aq]
  167. message=\[aq]message\[aq]
  168. source=\[aq]ShellCheck.SC####\[aq] />
  169. ...
  170. </file>
  171. ...
  172. </checkstyle>
  173. \f[R]
  174. .fi
  175. .RE
  176. .TP
  177. \f[B]diff\f[R]
  178. Auto-fixes in unified diff format.
  179. Can be piped to \f[C]git apply\f[R] or \f[C]patch -p1\f[R] to
  180. automatically apply fixes.
  181. .RS
  182. .IP
  183. .nf
  184. \f[C]
  185. --- a/test.sh
  186. +++ b/test.sh
  187. \[at]\[at] -2,6 +2,6 \[at]\[at]
  188. ## Example of a broken script.
  189. for f in $(ls *.m3u)
  190. do
  191. - grep -qi hq.*mp3 $f \[rs]
  192. + grep -qi hq.*mp3 \[dq]$f\[dq] \[rs]
  193. && echo -e \[aq]Playlist $f contains a HQ file in mp3 format\[aq]
  194. done
  195. \f[R]
  196. .fi
  197. .RE
  198. .TP
  199. \f[B]json1\f[R]
  200. Json is a popular serialization format that is more suitable for web
  201. applications.
  202. ShellCheck\[aq]s json is compact and contains only the bare minimum.
  203. Tabs are counted as 1 character.
  204. .RS
  205. .IP
  206. .nf
  207. \f[C]
  208. {
  209. comments: [
  210. {
  211. \[dq]file\[dq]: \[dq]filename\[dq],
  212. \[dq]line\[dq]: lineNumber,
  213. \[dq]column\[dq]: columnNumber,
  214. \[dq]level\[dq]: \[dq]severitylevel\[dq],
  215. \[dq]code\[dq]: errorCode,
  216. \[dq]message\[dq]: \[dq]warning message\[dq]
  217. },
  218. ...
  219. ]
  220. }
  221. \f[R]
  222. .fi
  223. .RE
  224. .TP
  225. \f[B]json\f[R]
  226. This is a legacy version of the \f[B]json1\f[R] format.
  227. It\[aq]s a raw array of comments, and all offsets have a tab stop of 8.
  228. .TP
  229. \f[B]quiet\f[R]
  230. Suppress all normal output.
  231. Exit with zero if no issues are found, otherwise exit with one.
  232. Stops processing after the first issue.
  233. .SH DIRECTIVES
  234. .PP
  235. ShellCheck directives can be specified as comments in the shell script.
  236. If they appear before the first command, they are considered file-wide.
  237. Otherwise, they apply to the immediately following command or block:
  238. .IP
  239. .nf
  240. \f[C]
  241. # shellcheck key=value key=value
  242. command-or-structure
  243. \f[R]
  244. .fi
  245. .PP
  246. For example, to suppress SC2035 about using \f[C]./*.jpg\f[R]:
  247. .IP
  248. .nf
  249. \f[C]
  250. # shellcheck disable=SC2035
  251. echo \[dq]Files: \[dq] *.jpg
  252. \f[R]
  253. .fi
  254. .PP
  255. To tell ShellCheck where to look for an otherwise dynamically determined
  256. file:
  257. .IP
  258. .nf
  259. \f[C]
  260. # shellcheck source=./lib.sh
  261. source \[dq]$(find_install_dir)/lib.sh\[dq]
  262. \f[R]
  263. .fi
  264. .PP
  265. Here a shell brace group is used to suppress a warning on multiple
  266. lines:
  267. .IP
  268. .nf
  269. \f[C]
  270. # shellcheck disable=SC2016
  271. {
  272. echo \[aq]Modifying $PATH\[aq]
  273. echo \[aq]PATH=foo:$PATH\[aq] >> \[ti]/.bashrc
  274. }
  275. \f[R]
  276. .fi
  277. .PP
  278. Valid keys are:
  279. .TP
  280. \f[B]disable\f[R]
  281. Disables a comma separated list of error codes for the following
  282. command.
  283. The command can be a simple command like \f[C]echo foo\f[R], or a
  284. compound command like a function definition, subshell block or loop.
  285. A range can be be specified with a dash, e.g.
  286. \f[C]disable=SC3000-SC4000\f[R] to exclude 3xxx.
  287. All warnings can be disabled with \f[C]disable=all\f[R].
  288. .TP
  289. \f[B]enable\f[R]
  290. Enable an optional check by name, as listed with
  291. \f[B]--list-optional\f[R].
  292. Only file-wide \f[C]enable\f[R] directives are considered.
  293. .TP
  294. \f[B]external-sources\f[R]
  295. Set to \f[C]true\f[R] in \f[C].shellcheckrc\f[R] to always allow
  296. ShellCheck to open arbitrary files from \[aq]source\[aq] statements (the
  297. way most tools do).
  298. .RS
  299. .PP
  300. This option defaults to \f[C]false\f[R] only due to ShellCheck\[aq]s
  301. origin as a remote service for checking untrusted scripts.
  302. It can safely be enabled for normal development.
  303. .RE
  304. .TP
  305. \f[B]source\f[R]
  306. Overrides the filename included by a \f[C]source\f[R]/\f[C].\f[R]
  307. statement.
  308. This can be used to tell shellcheck where to look for a file whose name
  309. is determined at runtime, or to skip a source by telling it to use
  310. \f[C]/dev/null\f[R].
  311. .TP
  312. \f[B]source-path\f[R]
  313. Add a directory to the search path for \f[C]source\f[R]/\f[C].\f[R]
  314. statements (by default, only ShellCheck\[aq]s working directory is
  315. included).
  316. Absolute paths will also be rooted in these paths.
  317. The special path \f[C]SCRIPTDIR\f[R] can be used to specify the
  318. currently checked script\[aq]s directory, as in
  319. \f[C]source-path=SCRIPTDIR\f[R] or
  320. \f[C]source-path=SCRIPTDIR/../libs\f[R].
  321. Multiple paths accumulate, and \f[C]-P\f[R] takes precedence over them.
  322. .TP
  323. \f[B]shell\f[R]
  324. Overrides the shell detected from the shebang.
  325. This is useful for files meant to be included (and thus lacking a
  326. shebang), or possibly as a more targeted alternative to
  327. \[aq]disable=SC2039\[aq].
  328. .SH RC FILES
  329. .PP
  330. Unless \f[C]--norc\f[R] is used, ShellCheck will look for a file
  331. \f[C].shellcheckrc\f[R] or \f[C]shellcheckrc\f[R] in the script\[aq]s
  332. directory and each parent directory.
  333. If found, it will read \f[C]key=value\f[R] pairs from it and treat them
  334. as file-wide directives.
  335. .PP
  336. Here is an example \f[C].shellcheckrc\f[R]:
  337. .IP
  338. .nf
  339. \f[C]
  340. # Look for \[aq]source\[aq]d files relative to the checked script,
  341. # and also look for absolute paths in /mnt/chroot
  342. source-path=SCRIPTDIR
  343. source-path=/mnt/chroot
  344. # Allow opening any \[aq]source\[aq]d file, even if not specified as input
  345. external-sources=true
  346. # Turn on warnings for unquoted variables with safe values
  347. enable=quote-safe-variables
  348. # Turn on warnings for unassigned uppercase variables
  349. enable=check-unassigned-uppercase
  350. # Allow [ ! -z foo ] instead of suggesting -n
  351. disable=SC2236
  352. \f[R]
  353. .fi
  354. .PP
  355. If no \f[C].shellcheckrc\f[R] is found in any of the parent directories,
  356. ShellCheck will look in \f[C]\[ti]/.shellcheckrc\f[R] followed by the
  357. XDG config directory (usually \f[C]\[ti]/.config/shellcheckrc\f[R]) on
  358. Unix, or \f[C]%APPDATA%/shellcheckrc\f[R] on Windows.
  359. Only the first file found will be used.
  360. .PP
  361. Note for Snap users: the Snap sandbox disallows access to hidden files.
  362. Use \f[C]shellcheckrc\f[R] without the dot instead.
  363. .PP
  364. Note for Docker users: ShellCheck will only be able to look for files
  365. that are mounted in the container, so \f[C]\[ti]/.shellcheckrc\f[R] will
  366. not be read.
  367. .SH ENVIRONMENT VARIABLES
  368. .PP
  369. The environment variable \f[C]SHELLCHECK_OPTS\f[R] can be set with
  370. default flags:
  371. .IP
  372. .nf
  373. \f[C]
  374. export SHELLCHECK_OPTS=\[aq]--shell=bash --exclude=SC2016\[aq]
  375. \f[R]
  376. .fi
  377. .PP
  378. Its value will be split on spaces and prepended to the command line on
  379. each invocation.
  380. .SH RETURN VALUES
  381. .PP
  382. ShellCheck uses the following exit codes:
  383. .IP \[bu] 2
  384. 0: All files successfully scanned with no issues.
  385. .IP \[bu] 2
  386. 1: All files successfully scanned with some issues.
  387. .IP \[bu] 2
  388. 2: Some files could not be processed (e.g.
  389. file not found).
  390. .IP \[bu] 2
  391. 3: ShellCheck was invoked with bad syntax (e.g.
  392. unknown flag).
  393. .IP \[bu] 2
  394. 4: ShellCheck was invoked with bad options (e.g.
  395. unknown formatter).
  396. .SH LOCALE
  397. .PP
  398. This version of ShellCheck is only available in English.
  399. All files are leniently decoded as UTF-8, with a fallback of ISO-8859-1
  400. for invalid sequences.
  401. \f[C]LC_CTYPE\f[R] is respected for output, and defaults to UTF-8 for
  402. locales where encoding is unspecified (such as the \f[C]C\f[R] locale).
  403. .PP
  404. Windows users seeing
  405. \f[C]commitBuffer: invalid argument (invalid character)\f[R] should set
  406. their terminal to use UTF-8 with \f[C]chcp 65001\f[R].
  407. .SH KNOWN INCOMPATIBILITIES
  408. .PP
  409. (If nothing in this section makes sense, you are unlikely to be affected
  410. by it)
  411. .PP
  412. To avoid confusing and misguided suggestions, ShellCheck requires
  413. function bodies to be either \f[C]{ brace groups; }\f[R] or
  414. \f[C]( subshells )\f[R], and function names containing \f[C][]*=!\f[R]
  415. are only recognized after a \f[C]function\f[R] keyword.
  416. .PP
  417. The following unconventional function definitions are identical in Bash,
  418. but ShellCheck only recognizes the latter.
  419. .IP
  420. .nf
  421. \f[C]
  422. [x!=y] () [[ $1 ]]
  423. function [x!=y] () { [[ $1 ]]; }
  424. \f[R]
  425. .fi
  426. .PP
  427. Shells without the \f[C]function\f[R] keyword do not allow these
  428. characters in function names to begin with.
  429. Function names containing \f[C]{}\f[R] are not supported at all.
  430. .PP
  431. Further, if ShellCheck sees \f[C][x!=y]\f[R] it will assume this is an
  432. invalid comparison.
  433. To invoke the above function, quote the command as in
  434. \f[C]\[aq][x!=y]\[aq]\f[R], or to retain the same globbing behavior, use
  435. \f[C]command [x!=y]\f[R].
  436. .PP
  437. ShellCheck imposes additional restrictions on the \f[C][\f[R] command to
  438. help diagnose common invalid uses.
  439. While \f[C][ $x= 1 ]\f[R] is defined in POSIX, ShellCheck will assume it
  440. was intended as the much more likely comparison
  441. \f[C][ \[dq]$x\[dq] = 1 ]\f[R] and fail accordingly.
  442. For unconventional or dynamic uses of the \f[C][\f[R] command, use
  443. \f[C]test\f[R] or \f[C]\[rs][\f[R] instead.
  444. .SH REPORTING BUGS
  445. .PP
  446. Bugs and issues can be reported on GitHub:
  447. .PP
  448. https://github.com/koalaman/shellcheck/issues
  449. .SH AUTHORS
  450. .PP
  451. ShellCheck is developed and maintained by Vidar Holen, with assistance
  452. from a long list of wonderful contributors.
  453. .SH COPYRIGHT
  454. .PP
  455. Copyright 2012-2021, Vidar Holen and contributors.
  456. Licensed under the GNU General Public License version 3 or later, see
  457. https://gnu.org/licenses/gpl.html
  458. .SH SEE ALSO
  459. .PP
  460. sh(1) bash(1)