• Stars
    star
    7,112
  • Rank 5,479 (Top 0.2 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created almost 9 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A shell parser, formatter, and interpreter with bash support; includes shfmt

sh

Go Reference

A shell parser, formatter, and interpreter. Supports POSIX Shell, Bash, and mksh. Requires Go 1.21 or later.

Quick start

To parse shell scripts, inspect them, and print them out, see the syntax examples.

For high-level operations like performing shell expansions on strings, see the shell examples.

shfmt

go install mvdan.cc/sh/v3/cmd/shfmt@latest

shfmt formats shell programs. See canonical.sh for a quick look at its default style. For example:

shfmt -l -w script.sh

For more information, see its manpage, which can be viewed directly as Markdown or rendered with scdoc.

Packages are available on Alpine, Arch, Debian, Docker, Fedora, FreeBSD, Homebrew, MacPorts, NixOS, OpenSUSE, Scoop, Snapcraft, Void and webi.

gosh

go install mvdan.cc/sh/v3/cmd/gosh@latest

Proof of concept shell that uses interp. Note that it's not meant to replace a POSIX shell at the moment, and its options are intentionally minimalistic.

Fuzzing

We use Go's native fuzzing support. For instance:

cd syntax
go test -run=- -fuzz=ParsePrint

Caveats

  • When indexing Bash associative arrays, always use quotes. The static parser will otherwise have to assume that the index is an arithmetic expression.
$ echo '${array[spaced string]}' | shfmt
1:16: not a valid arithmetic operator: string
$ echo '${array[dash-string]}' | shfmt
${array[dash - string]}
  • $(( and (( ambiguity is not supported. Backtracking would complicate the parser and make streaming support via io.Reader impossible. The POSIX spec recommends to space the operands if $( ( is meant.
$ echo '$((foo); (bar))' | shfmt
1:1: reached ) without matching $(( with ))
  • Some builtins like export and let are parsed as keywords. This allows statically building their syntax tree, as opposed to keeping the arguments as a slice of words. It is also required to support declare foo=(bar). Note that this means expansions like declare {a,b}=c are not supported.

JavaScript

A subset of the Go packages are available as an npm package called mvdan-sh. See the _js directory for more information.

Docker

All release tags are published via Docker, such as v3.5.1. The latest stable release is currently published as v3, and the latest development version as latest. The images only include shfmt; -alpine variants exist on Alpine Linux.

To build a Docker image, run:

docker build -t my:tag -f cmd/shfmt/Dockerfile .

To use a Docker image, run:

docker run --rm -u "$(id -u):$(id -g)" -v "$PWD:/mnt" -w /mnt my:tag <shfmt arguments>

Related projects

The following editor integrations wrap shfmt:

Other noteworthy integrations include:

More Repositories

1

gofumpt

A stricter gofmt
Go
3,184
star
2

xurls

Extract urls from text
Go
1,175
star
3

github-actions-golang

GitHub Actions as CI for Go
Go
1,025
star
4

interfacer

A linter that suggests interface types
Go
690
star
5

unparam

Find unused parameters in Go
Go
527
star
6

gogrep

Search for Go code using syntax trees
Go
477
star
7

fdroidcl

F-Droid desktop client
Go
262
star
8

goreduce

Reduce Go programs
Go
216
star
9

bitw

Minimalist BitWarden client
Go
168
star
10

zstd

Zstandard implementation in Wuffs
C
112
star
11

corpus

A corpus of popular Go modules
Go
106
star
12

dockexec

Run Go tests inside a Docker image
Go
98
star
13

accesspoint

Manage wireless access points in Android (abandoned)
Java
73
star
14

benchinit

Benchmark the init cost of Go packages
Go
72
star
15

pastecat

Pastebin service (abandoned)
Go
36
star
16

git-picked

List merged and cherry-picked branches
Go
29
star
17

winup

Automate a Windows 10 VM setup for coding and testing
Go
21
star
18

unindent

Report code that is unnecessarily indented
Go
19
star
19

android-template

Android app template
Java
16
star
20

dotfiles

Here be dragons
Shell
14
star
21

talks

Collection of slides from talks
Go
9
star
22

go-concurrency-workshop

Go
9
star
23

editorconfig

EditorConfig support in Go
Go
8
star
24

lint

Common interfaces for Go code checkers
Go
6
star
25

macfuzzer

Android MAC changer and randomizer (abandoned)
Java
6
star
26

gexf

GEXF file format implementation
Go
3
star
27

mvdan.cc

Personal website
HTML
3
star
28

playtyk

This repo is a temporary home for a hack.
Go
3
star
29

responsefile

Support for response files in Go, to bypass argument length limits
Go
2
star
30

tor

Consensus diffs Tor GSoC project
C
1
star
31

route

portable Go package to obtain simple routing information
Go
1
star
32

nowt

Nothing extraordinary here
Go
1
star
33

basedir

Platform-specific base directories
Go
1
star