| 1 |
# sh |
| 2 |
|
| 3 |
[](https://pkg.go.dev/mvdan.cc/sh/v3) |
| 4 |
|
| 5 |
A shell parser, formatter, and interpreter. |
| 6 |
Supports [POSIX Shell], [Bash], [Zsh], and [mksh]. Requires Go 1.26 or later. |
| 7 |
|
| 8 |
### Quick start |
| 9 |
|
| 10 |
To parse shell scripts, inspect them, and print them out, |
| 11 |
see the [syntax package](https://pkg.go.dev/mvdan.cc/sh/v3/syntax). |
| 12 |
|
| 13 |
For high-level operations like performing shell expansions on strings, |
| 14 |
see the [shell package](https://pkg.go.dev/mvdan.cc/sh/v3/shell). |
| 15 |
|
| 16 |
To interpret or run shell scripts, |
| 17 |
see the [interp package](https://pkg.go.dev/mvdan.cc/sh/v3/interp). |
| 18 |
|
| 19 |
### shfmt |
| 20 |
|
| 21 |
go install mvdan.cc/sh/v3/cmd/shfmt@latest |
| 22 |
|
| 23 |
`shfmt` formats shell programs. See [canonical.sh](syntax/canonical.sh) for a |
| 24 |
quick look at its default style. For example: |
| 25 |
|
| 26 |
shfmt -l -w script.sh |
| 27 |
|
| 28 |
For more information, see [its manpage](cmd/shfmt/shfmt.1.scd), which can be |
| 29 |
viewed directly as Markdown or rendered with [scdoc]. |
| 30 |
|
| 31 |
Packages are available on [Alpine], [Arch], [Debian], [Docker], [Fedora], [FreeBSD], |
| 32 |
[Homebrew], [MacPorts], [NixOS], [OpenSUSE], [Scoop], [Snapcraft], [Void] and [webi]. |
| 33 |
|
| 34 |
### Sponsoring |
| 35 |
|
| 36 |
If this project saves you or your company time, consider |
| 37 |
[sponsoring me on GitHub](https://github.com/sponsors/mvdan). |
| 38 |
Monthly tiers include benefits like your logo on a README, |
| 39 |
prioritized issues, or direct support in your company's chat app. |
| 40 |
One-time tiers offer a call about one of my projects |
| 41 |
or a Go consulting or mentorship session. |
| 42 |
|
| 43 |
### Contributing |
| 44 |
|
| 45 |
Bug reports and feature requests should be filed as detailed issues, |
| 46 |
ideally with an example which reproduces the bug or shows what feature you're after. |
| 47 |
|
| 48 |
Unless you're an active user or contributor to the project, drive-by AI patches |
| 49 |
are not helpful. File detailed issues instead. |
| 50 |
|
| 51 |
### Caveats |
| 52 |
|
| 53 |
* When indexing Bash associative arrays, always use quotes. The static parser |
| 54 |
will otherwise have to assume that the index is an arithmetic expression. |
| 55 |
|
| 56 |
```sh |
| 57 |
$ echo '${array[spaced string]}' | shfmt |
| 58 |
<standard input>:1:16: not a valid arithmetic operator: `string` |
| 59 |
$ echo '${array[weird!key]}' | shfmt |
| 60 |
<standard input>:1:8: reached `!` without matching `[` with `]` |
| 61 |
$ echo '${array[dash-string]}' | shfmt |
| 62 |
${array[dash - string]} |
| 63 |
``` |
| 64 |
|
| 65 |
* `$((` and `((` ambiguity is not supported. Backtracking would complicate the |
| 66 |
parser and make streaming support via `io.Reader` impossible. The POSIX spec |
| 67 |
recommends to [space the operands][posix-ambiguity] if `$( (` is meant. |
| 68 |
|
| 69 |
```sh |
| 70 |
$ echo '$((foo); (bar))' | shfmt |
| 71 |
1:1: reached ) without matching $(( with )) |
| 72 |
``` |
| 73 |
|
| 74 |
* `export`, `let`, and `declare` are parsed as keywords. |
| 75 |
This allows statically building their syntax tree, |
| 76 |
as opposed to keeping the arguments as a slice of words. |
| 77 |
It is also required to support `declare foo=(bar)`. |
| 78 |
|
| 79 |
* The entire library is written in pure Go, which limits how closely the |
| 80 |
interpreter can follow POSIX Shell and Bash semantics. |
| 81 |
For example, Go does not support forking its own process, so subshells |
| 82 |
use a goroutine instead, meaning that real PIDs and file descriptors |
| 83 |
cannot be used directly. |
| 84 |
|
| 85 |
### Formatting FAQs |
| 86 |
|
| 87 |
* The formatter cannot be disabled for ranges of lines; most users wanting this |
| 88 |
are working around a bug or they don't like how a piece of code is formatted. |
| 89 |
Instead, search the issue tracker and file a new issue if necessary. |
| 90 |
Formatting of partial files leads to lots of edge cases and complexity |
| 91 |
which this project has no resources for, nor interest in, getting into. |
| 92 |
|
| 93 |
* We avoid adding more formatting options where possible. Each added flag interacts |
| 94 |
with all others, multiplying the human cost of development, maintenance, testing, |
| 95 |
and properly documenting the behavior for end users. |
| 96 |
|
| 97 |
* The true value in a formatter is consistency, especially for teams of developers. |
| 98 |
We do not aim to satisfy every developer's personal choice of optimal formatting. |
| 99 |
|
| 100 |
### JavaScript |
| 101 |
|
| 102 |
The parser and formatter are available as a third party npm package called [sh-syntax], |
| 103 |
which bundles a version of this library compiled to WASM. |
| 104 |
|
| 105 |
Previously, we maintained an npm package called [mvdan-sh] which used GopherJS |
| 106 |
to bundle a JS version of this library. That npm package is now archived |
| 107 |
given its poor performance and GopherJS not being as actively developed. |
| 108 |
Any existing or new users should look at [sh-syntax] instead. |
| 109 |
|
| 110 |
### Docker |
| 111 |
|
| 112 |
All release tags are published via [Docker], such as `v3.5.1`. |
| 113 |
The latest stable release is currently published as `v3`, |
| 114 |
and the latest development version as `latest`. |
| 115 |
The images only include `shfmt`; `-alpine` variants exist on Alpine Linux. |
| 116 |
|
| 117 |
To build a Docker image, run: |
| 118 |
|
| 119 |
docker build -t my:tag -f cmd/shfmt/Dockerfile . |
| 120 |
|
| 121 |
To use a Docker image, run: |
| 122 |
|
| 123 |
docker run --rm -u "$(id -u):$(id -g)" -v "$PWD:/mnt" -w /mnt my:tag <shfmt arguments> |
| 124 |
|
| 125 |
### Related projects |
| 126 |
|
| 127 |
The following editor integrations wrap `shfmt`: |
| 128 |
|
| 129 |
- [BashSupport-Pro] - Bash plugin for JetBrains IDEs |
| 130 |
- [dockerfmt] - Dockerfile formatter using shfmt |
| 131 |
- [intellij-shellscript] - Intellij Jetbrains `shell script` plugin |
| 132 |
- [micro] - Editor with a built-in plugin |
| 133 |
- [neoformat] - (Neo)Vim plugin |
| 134 |
- [vscode-shfmt] - VS Code plugin |
| 135 |
- [shfmt.el] - Emacs package |
| 136 |
- [Trunk] - Universal linter, available as a CLI, VS Code plugin, and GitHub action |
| 137 |
|
| 138 |
Other noteworthy integrations include: |
| 139 |
|
| 140 |
- [modd] - A developer tool that responds to filesystem changes |
| 141 |
- [prettier-plugin-sh] - [Prettier] plugin using [sh-syntax] |
| 142 |
- [sh-checker] - A GitHub Action that performs static analysis for shell scripts |
| 143 |
- [mdformat-shfmt] - [mdformat] plugin to format shell scripts embedded in Markdown with shfmt |
| 144 |
- [pre-commit-shfmt] - [pre-commit] shfmt hook |
| 145 |
- [tesh] - Run scripts with mocks, assertions, and coverage |
| 146 |
|
| 147 |
[alpine]: https://pkgs.alpinelinux.org/packages?name=shfmt |
| 148 |
[arch]: https://archlinux.org/packages/extra/x86_64/shfmt/ |
| 149 |
[bash]: https://www.gnu.org/software/bash/ |
| 150 |
[BashSupport-Pro]: https://www.bashsupport.com/manual/editor/formatter/ |
| 151 |
[debian]: https://tracker.debian.org/pkg/golang-mvdan-sh |
| 152 |
[docker]: https://hub.docker.com/r/mvdan/shfmt/ |
| 153 |
[dockerfmt]: https://github.com/reteps/dockerfmt |
| 154 |
[editorconfig]: https://editorconfig.org/ |
| 155 |
[examples]: https://pkg.go.dev/mvdan.cc/sh/v3/syntax#pkg-examples |
| 156 |
[fedora]: https://packages.fedoraproject.org/pkgs/golang-mvdan-sh-3/shfmt/ |
| 157 |
[freebsd]: https://www.freshports.org/devel/shfmt |
| 158 |
[homebrew]: https://formulae.brew.sh/formula/shfmt |
| 159 |
[intellij-shellscript]: https://www.jetbrains.com/help/idea/shell-scripts.html |
| 160 |
[macports]: https://ports.macports.org/port/shfmt/details/ |
| 161 |
[mdformat-shfmt]: https://github.com/hukkin/mdformat-shfmt |
| 162 |
[mdformat]: https://github.com/executablebooks/mdformat |
| 163 |
[micro]: https://micro-editor.github.io/ |
| 164 |
[mksh]: http://www.mirbsd.org/mksh.htm |
| 165 |
[modd]: https://github.com/cortesi/modd |
| 166 |
[mvdan-sh]: https://www.npmjs.com/package/mvdan-sh |
| 167 |
[neoformat]: https://github.com/sbdchd/neoformat |
| 168 |
[nixos]: https://github.com/NixOS/nixpkgs/blob/HEAD/pkgs/tools/text/shfmt/default.nix |
| 169 |
[OpenSUSE]: https://build.opensuse.org/package/show/openSUSE:Factory/shfmt |
| 170 |
[posix shell]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html |
| 171 |
[posix-ambiguity]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03 |
| 172 |
[pre-commit]: https://pre-commit.com |
| 173 |
[pre-commit-shfmt]: https://github.com/scop/pre-commit-shfmt |
| 174 |
[prettier-plugin-sh]: https://github.com/un-ts/prettier/tree/master/packages/sh |
| 175 |
[prettier]: https://prettier.io |
| 176 |
[scdoc]: https://sr.ht/~sircmpwn/scdoc/ |
| 177 |
[scoop]: https://github.com/ScoopInstaller/Main/blob/HEAD/bucket/shfmt.json |
| 178 |
[sh-checker]: https://github.com/luizm/action-sh-checker |
| 179 |
[sh-syntax]: https://github.com/un-ts/sh-syntax |
| 180 |
[shfmt.el]: https://github.com/purcell/emacs-shfmt/ |
| 181 |
[snapcraft]: https://snapcraft.io/shfmt |
| 182 |
[tesh]: https://github.com/feloy/tesh |
| 183 |
[trunk]: https://trunk.io/check |
| 184 |
[void]: https://github.com/void-linux/void-packages/blob/HEAD/srcpkgs/shfmt/template |
| 185 |
[vscode-shfmt]: https://marketplace.visualstudio.com/items?itemName=mkhl.shfmt |
| 186 |
[webi]: https://webinstall.dev/shfmt/ |
| 187 |
[Zsh]: https://www.zsh.org/ |