shfmt is a command line program - A shell parser, formatter and interpreter (POSIX/Bash/mksh). This tool can be called directly on the command line or added to some editors (such as vscode with an extension like https://marketplace.visualstudio.com/items?itemName=foxundermoon.shell-format).
NOTE - Currently this snap is in strict confinement with the home plug. So obviously your code needs to be in your home directory. Open to suggestions if there are improvements for this (i.e. - to account for those who do not store their code in their home directory) without requesting classic confinement.
EDIT - ok, the vscode stuff needs to hold up for the moment. I just realized there are access issues going from the snap to a snap. Likely need another plug (suggestions in case I don’t find the answer right away?)
How to install
sudo snap install shfmt
Since this is a go binary, there are no external dependencies. I would say at a minimum, test at the terminal. If so inclined, a test within vscode would not hurt.
- install vscode if you do not have it already with
sudo snap install vscode --classic
- Install the vscode shell-format extension with
code --install-extension foxundermoon.shell-format
- Open your vscode settings.json and add
"shellformat.path": "/snap/bin/shfmt". Keep in mind that you would need a comma at the end if you are not adding this at the end of your config.
- Run against a shell script (with an intentional error) at the terminal with
shfmt -l -w bad.sh
- Open the same bad script in vscode and ensure the same errors appear in the lower left corner.
- If running against a proper shell script, it should return with a blank line.
- If running aginst a bad shell script, it should return an error with the results such as:
$ shfmt -l -w bad.sh bad.sh:7:1: if statement must end with "fi"
Output of “snap version”
$ snap version snap 2.35.4 snapd 2.35.4 series 16 ubuntu 18.04 kernel 4.15.0-38-generic
Output of “dmesg” when application is launched, which can be useful when debugging confinement issues.
vscode shfmt dmesg errors
[ 2908.781603] audit: type=1400 audit(1540537254.332:540): apparmor="DENIED" operation="file_inherit" profile="/snap/core/5662/usr/lib/snapd/snap-confine" pid=9870 comm="snap-confine" family="unix" sock_type="stream" protocol=0 requested_mask="send receive" denied_mask="send receive" addr=none peer_addr=none