{"id":13413974,"url":"https://github.com/mvdan/sh","last_synced_at":"2025-05-12T16:34:10.323Z","repository":{"id":37412677,"uuid":"49766020","full_name":"mvdan/sh","owner":"mvdan","description":"A shell parser, formatter, and interpreter with bash support; includes shfmt","archived":false,"fork":false,"pushed_at":"2025-04-23T21:28:45.000Z","size":5651,"stargazers_count":7698,"open_issues_count":94,"forks_count":361,"subscribers_count":62,"default_branch":"master","last_synced_at":"2025-05-05T14:16:37.660Z","etag":null,"topics":["bash","beautify","formatter","go","interpreter","mksh","parser","posix","shell","shfmt"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/mvdan.cc/sh/v3","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mvdan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"mvdan"}},"created_at":"2016-01-16T08:39:09.000Z","updated_at":"2025-05-05T11:22:44.000Z","dependencies_parsed_at":"2022-07-08T02:58:16.440Z","dependency_job_id":"d7a83068-0263-41a9-bb6c-c8dc79cbd5af","html_url":"https://github.com/mvdan/sh","commit_stats":{"total_commits":3618,"total_committers":82,"mean_commits":44.1219512195122,"dds":"0.050027639579878436","last_synced_commit":"793d36450a0a5a466199b04c458fb4a1202cf641"},"previous_names":[],"tags_count":59,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvdan%2Fsh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvdan%2Fsh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvdan%2Fsh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvdan%2Fsh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mvdan","download_url":"https://codeload.github.com/mvdan/sh/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252569724,"owners_count":21769531,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bash","beautify","formatter","go","interpreter","mksh","parser","posix","shell","shfmt"],"created_at":"2024-07-30T20:01:54.169Z","updated_at":"2025-05-12T16:34:10.297Z","avatar_url":"https://github.com/mvdan.png","language":"Go","readme":"# sh\n\n[![Go Reference](https://pkg.go.dev/badge/mvdan.cc/sh/v3.svg)](https://pkg.go.dev/mvdan.cc/sh/v3)\n\nA shell parser, formatter, and interpreter. Supports [POSIX Shell], [Bash], and\n[mksh]. Requires Go 1.23 or later.\n\n### Quick start\n\nTo parse shell scripts, inspect them, and print them out, see the [syntax\npackage examples](https://pkg.go.dev/mvdan.cc/sh/v3/syntax#pkg-examples).\n\nFor high-level operations like performing shell expansions on strings, see the\n[shell package examples](https://pkg.go.dev/mvdan.cc/sh/v3/shell#pkg-examples).\n\n### shfmt\n\n\tgo install mvdan.cc/sh/v3/cmd/shfmt@latest\n\n`shfmt` formats shell programs. See [canonical.sh](syntax/canonical.sh) for a\nquick look at its default style. For example:\n\n\tshfmt -l -w script.sh\n\nFor more information, see [its manpage](cmd/shfmt/shfmt.1.scd), which can be\nviewed directly as Markdown or rendered with [scdoc].\n\nPackages are available on [Alpine], [Arch], [Debian], [Docker], [Fedora], [FreeBSD],\n[Homebrew], [MacPorts], [NixOS], [OpenSUSE], [Scoop], [Snapcraft], [Void] and [webi].\n\n### gosh\n\n\tgo install mvdan.cc/sh/v3/cmd/gosh@latest\n\nProof of concept shell that uses `interp`. Note that it's not meant to replace a\nPOSIX shell at the moment, and its options are intentionally minimalistic.\n\n### Fuzzing\n\nWe use Go's native fuzzing support. For instance:\n\n\tcd syntax\n\tgo test -run=- -fuzz=ParsePrint\n\n### Caveats\n\n* When indexing Bash associative arrays, always use quotes. The static parser\n  will otherwise have to assume that the index is an arithmetic expression.\n\n```sh\n$ echo '${array[spaced string]}' | shfmt\n1:16: not a valid arithmetic operator: string\n$ echo '${array[dash-string]}' | shfmt\n${array[dash - string]}\n```\n\n* `$((` and `((` ambiguity is not supported. Backtracking would complicate the\n  parser and make streaming support via `io.Reader` impossible. The POSIX spec\n  recommends to [space the operands][posix-ambiguity] if `$( (` is meant.\n\n```sh\n$ echo '$((foo); (bar))' | shfmt\n1:1: reached ) without matching $(( with ))\n```\n\n* `export`, `let`, and `declare` are parsed as keywords.\n  This allows statically building their syntax tree,\n  as opposed to keeping the arguments as a slice of words.\n  It is also required to support `declare foo=(bar)`.\n  Note that this means expansions like `declare {a,b}=c` are not supported.\n\n* The entire library is written in pure Go, which limits how closely the\n  interpreter can follow POSIX Shell and Bash semantics.\n  For example, Go does not support forking its own process, so subshells\n  use a goroutine instead, meaning that real PIDs and file descriptors\n  cannot be used directly.\n\n### JavaScript\n\nThe parser and formatter are available as a third party npm package called [sh-syntax],\nwhich bundles a version of this library compiled to WASM.\n\nPreviously, we maintained an npm package called [mvdan-sh] which used GopherJS\nto bundle a JS version of this library. That npm package is now archived\ngiven its poor performance and GopherJS not being as actively developed.\nAny existing or new users should look at [sh-syntax] instead.\n\n### Docker\n\nAll release tags are published via [Docker], such as `v3.5.1`.\nThe latest stable release is currently published as `v3`,\nand the latest development version as `latest`.\nThe images only include `shfmt`; `-alpine` variants exist on Alpine Linux.\n\nTo build a Docker image, run:\n\n\tdocker build -t my:tag -f cmd/shfmt/Dockerfile .\n\nTo use a Docker image, run:\n\n\tdocker run --rm -u \"$(id -u):$(id -g)\" -v \"$PWD:/mnt\" -w /mnt my:tag \u003cshfmt arguments\u003e\n\n### Related projects\n\nThe following editor integrations wrap `shfmt`:\n\n- [BashSupport-Pro] - Bash plugin for JetBrains IDEs\n- [dockerfmt] - Dockerfile formatter using shfmt\n- [intellij-shellcript] - Intellij Jetbrains `shell script` plugin\n- [micro] - Editor with a built-in plugin\n- [neoformat] - (Neo)Vim plugin\n- [shell-format] - VS Code plugin\n- [vscode-shfmt] - VS Code plugin\n- [shfmt.el] - Emacs package\n- [Sublime-Pretty-Shell] - Sublime Text 3 plugin\n- [Trunk] - Universal linter, available as a CLI, VS Code plugin, and GitHub action\n- [vim-shfmt] - Vim plugin\n\nOther noteworthy integrations include:\n\n- [modd] - A developer tool that responds to filesystem changes\n- [prettier-plugin-sh] - [Prettier] plugin using [sh-syntax]\n- [sh-checker] - A GitHub Action that performs static analysis for shell scripts\n- [mdformat-shfmt] - [mdformat] plugin to format shell scripts embedded in Markdown with shfmt\n- [pre-commit-shfmt] - [pre-commit] shfmt hook\n\n[alpine]: https://pkgs.alpinelinux.org/packages?name=shfmt\n[arch]: https://archlinux.org/packages/extra/x86_64/shfmt/\n[bash]: https://www.gnu.org/software/bash/\n[BashSupport-Pro]: https://www.bashsupport.com/manual/editor/formatter/\n[debian]: https://tracker.debian.org/pkg/golang-mvdan-sh\n[docker]: https://hub.docker.com/r/mvdan/shfmt/\n[dockerfmt]: https://github.com/reteps/dockerfmt\n[editorconfig]: https://editorconfig.org/\n[examples]: https://pkg.go.dev/mvdan.cc/sh/v3/syntax#pkg-examples\n[fedora]: https://packages.fedoraproject.org/pkgs/golang-mvdan-sh-3/shfmt/\n[freebsd]: https://www.freshports.org/devel/shfmt\n[homebrew]: https://formulae.brew.sh/formula/shfmt\n[intellij-shellcript]: https://www.jetbrains.com/help/idea/shell-scripts.html\n[macports]: https://ports.macports.org/port/shfmt/details/\n[mdformat-shfmt]: https://github.com/hukkin/mdformat-shfmt\n[mdformat]: https://github.com/executablebooks/mdformat\n[micro]: https://micro-editor.github.io/\n[mksh]: http://www.mirbsd.org/mksh.htm\n[modd]: https://github.com/cortesi/modd\n[mvdan-sh]: https://www.npmjs.com/package/mvdan-sh\n[neoformat]: https://github.com/sbdchd/neoformat\n[nixos]: https://github.com/NixOS/nixpkgs/blob/HEAD/pkgs/tools/text/shfmt/default.nix\n[OpenSUSE]: https://build.opensuse.org/package/show/openSUSE:Factory/shfmt\n[posix shell]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html\n[posix-ambiguity]: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03\n[pre-commit]: https://pre-commit.com\n[pre-commit-shfmt]: https://github.com/scop/pre-commit-shfmt\n[prettier-plugin-sh]: https://github.com/un-ts/prettier/tree/master/packages/sh\n[prettier]: https://prettier.io\n[scdoc]: https://sr.ht/~sircmpwn/scdoc/\n[scoop]: https://github.com/ScoopInstaller/Main/blob/HEAD/bucket/shfmt.json\n[sh-checker]: https://github.com/luizm/action-sh-checker\n[sh-syntax]: https://github.com/un-ts/sh-syntax\n[shell-format]: https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format\n[shfmt.el]: https://github.com/purcell/emacs-shfmt/\n[snapcraft]: https://snapcraft.io/shfmt\n[sublime-pretty-shell]: https://github.com/aerobounce/Sublime-Pretty-Shell\n[trunk]: https://trunk.io/check\n[vim-shfmt]: https://github.com/z0mbix/vim-shfmt\n[void]: https://github.com/void-linux/void-packages/blob/HEAD/srcpkgs/shfmt/template\n[vscode-shfmt]: https://marketplace.visualstudio.com/items?itemName=mkhl.shfmt\n[webi]: https://webinstall.dev/shfmt/\n","funding_links":["https://github.com/sponsors/mvdan"],"categories":["Text Processing","Misc","语言资源库","Go","开源类库","Shell Script Development","Open source library","Containers \u0026 Language Extentions \u0026 Linting","Linters","终端","bash","go","Programming","文本处理","Template Engines","Productivity tools","Shell","\u003cspan id=\"文字处理-text-processing\"\u003e文字处理 Text Processing\u003c/span\u003e","DevOps","tools","Analysis","Bot Building","文本處理","文本处理`解析和操作文本的代码库`","Specific Formats"],"sub_categories":["HTTP Clients","go","解释器","Parsers/Encoders/Decoders","Directory Navigation","Interpreter","For Go","Shell","网络服务_其他","Advanced Console UIs","Linters / Formaters \u003c!-- omit in toc --\u003e","解析 器/Encoders/Decoders","Chess :chess_pawn:","高级控制台界面","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","交流","查询语","Reusable Things","高級控制台界面"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmvdan%2Fsh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmvdan%2Fsh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmvdan%2Fsh/lists"}