Let me present to you the (I don’t know how many, because I’m still writing this) points to survive in the world of shell scripting.
1. Shhhh…
If you’re new to shell scripting and have not scripted any bash, you should consider not doing it, at least for a while. Bash has many wonderful advanced techniques, but it’s also an advanced experience to read it.
Use that #!/bin/sh
shebang, and write your scripts in “Portable Operating System Interface for uniX” shell. This is what you should learn first, and it will give you the most portable scripts.
2. Use ShellCheck
I like to make fun of ShellCheck, with its not-so-verbose output and identifiers like SC1234, but it’s a great tool.
It’s a great tool, for learning and for security as well. When you write shell scripts, you’re automating your whole system with text manipulation. You should be careful.
3. Use shfmt if you’re lazy to format your scripts
As the title says, just do it.
Pro tip: If you are a dork and want to follow the Google Style Guide , call it like this:
shfmt -i 2 -ci
2. and 3. addendum
You can use bashls if you need a fully functional language server for a shell scripting language.
You can use ShellCheck and shfmt in your CI/CD pipelines, and maybe you should.
You can read the last point hoping you will learn something, but you won’t.
4. or 5. The nitpick
Do not use #!/usr/bin/env sh
.
You do not need an additional process for calling sh
. Use #!/bin/sh
instead. If your sh
executable is in some other path, you’re not reading this guide.
From a security standpoint, you should always know which exact interpreter you’re using.
As I predicted, you’re not learning shell scripting from these 4 or 5 points, but now you can start learning it. Good luck!