TIL about https://www.shellcheck.net, an open-source tool that can find bugs in your shellscripts!

… it certainly finds bugs in *my* shellscripts. /o\ #bangbangcon

ShellCheck – shell script analysis tool

ShellCheck finds bugs in your shell scripts

@blinry should be the minimal check on all your shellscripts :-)
@haploc @blinry Yes, this. #BashScripting is unsafe by default, though, so you really shouldn’t be doing anything terribly complicated in it unless you really can’t depend on another language or a C compiler being available in all cases. If you need to use Bash, you should follow a rigorous set of best practices and use Shellcheck to help enforce them. Here’s a good starting set of best practices - perhaps some day I’ll get some time to write mine down. https://bertvv.github.io/cheat-sheets/Bash.html
Bash best practices

Cheat sheets for various stuff

cheat-sheets
@blinry oh boy, I'm so never gonna use this on the stuff one scripted that actually works. They're cobbled together from SO posts and I don't grok the arcane details. One change and they could go kaboom

@alexcg @blinry This is 99% of all shell scripts everywhere.

I have shellcheck + flycheck in my editor and it gets really grumpy about everything. I do try to fix what I understand, though

@blinry there's a handy vscode plugin for this as well!
@blinry
We use it at work.😀
@blinry oh yes. I have it set up with the vim-syntastic plugin so it can criticise every line I write instantly! :3
@blinry It is so hard to write reliable #shell scripts, that i would recommend against it for anything that has to run out of one's sight.
@clonejo I agree! :D Sometimes, I start writing a Bash script, and then just change the shebang and do it in Ruby instead. :P
@blinry pre-commit repo has a hook for it. Searches for bash files and run shellcheck before a 'git commit'.
GitHub - anordal/shellharden: The corrective bash syntax highlighter

The corrective bash syntax highlighter. Contribute to anordal/shellharden development by creating an account on GitHub.

GitHub
@blinry My .bashrc produces hundreds of lines of output (since years, I never really bothered).
@blinry shfmt is a great autoformatter too https://github.com/mvdan/sh
GitHub - mvdan/sh: A shell parser, formatter, and interpreter with bash and zsh support; includes shfmt

A shell parser, formatter, and interpreter with bash and zsh support; includes shfmt - mvdan/sh

GitHub
@blinry It's really fantastic, and It has LSP support if you are into that sort of thing :)
@blinry Yeah, ShellCheck is great! Everyone writing shell scripts -- or at least shell scripts that other people will be running or contributing to -- should use it.
@blinry I owe my shell scripting knowledge to this tool! I actually enjoy when it shows me what I could do better.

@blinry

Spellcheck is "written in Haskell, if you're into that sort of thing."

Another similar tool is Hadolint at

https://github.com/hadolint/hadolint

"Dockerfile linter, validate inline bash, written in Haskell"

Linters are great especially when you use them at the start of a project!

[ #shellcheck #hadolint #haskell #bash ]

GitHub - hadolint/hadolint: Dockerfile linter, validate inline bash, written in Haskell

Dockerfile linter, validate inline bash, written in Haskell - hadolint/hadolint

GitHub
@w8emv @blinry The irony that your spellcheck changed ShellCheck to Spellcheck is not lost on me 😂

@Axman6 @blinry

Delicious, delicious irony.

I am still struggling with autocarrot on this here device, there is a keystroke combo ("right arrow") that will signal "yes I typed this right" but I don't always catch it.

@blinry @fasterthanlime

I always use shellcheck as well, and I spend the time saved finding more subtle problems.

@blinry `shellcheck` is a must for me plugged with ALE in Vim. Helps a lot with hair pulling mistakes for people like me not well versed in shell scripting.

@blinry might as well admit I'm going to keep writing .sh ...

# This is bash. Be safe out there.
set -e
set -u

@blinry after you're done fixing your scripts you can continue with your makefiles: https://github.com/mrtazz/checkmate :D
@blinry Double quote to prevent globbing and word-splitting

@blinry we use this in our Terraform and Ansible build pipelines at work.

Intellij also recommends when you open a file with a shell script File Type (either by extension or hash bang.)

Very useful tool.

@blinry Great tool - thank you for sharing! :)
@blinry the coolest part is that it checks the shebang and then applies the rules accordingly, e.g. if you want bash compatibility or posix!
@floyd @blinry shellcheck is my single go-to-source of wisdom to check if my bash spoiled scripts are actually portable to POSIX shells. ✌️
@blinry There are even Plugins for most IDEs. 😉
@blinry wow, this looks really helpful!