Four Tips for Better Bash Scripts

Al Williams
For Linux Users
Published in
2 min readMay 27, 2022

--

Plan, Test, Build GUIs, and Compile!

Photo by Catherine Ari Harthiany on Unsplash

If you are writing a shell script for yourself, that’s pretty easy. You’ll remember if you need to not have spaces in file names and you know all the programs your script needs are in place and the correct version. But if you are sending your script out into the world, that’s a different problem. Here are four tips to help you write better and more robust scripts.

1. Template

There are so many things to think about when you create a script. A template might help. At the very least, you can see what other people think they should guard against and see if you are doing it, too.

2. Lint

Are you old enough to remember lint for C? That was the program that looked at your C code and pointed out all the things a compiler would warn you about today (like wrong number of arguments to printf or mismatched data types). Well, there’s something like lint for bash, too. It will warn you about potential problems with your script before you run it.

3. GUI

I recently wanted to make fancy UI elements for a shell script. The problem is how? If you use KDE (I do), there’s KDialog, but many people have Zenity. And if you aren’t under X11, there are tools to do text-based UI elements. But they are all…

--

--

Al Williams
For Linux Users

Engineer. Author. Team Leader. Lots of other things. I blog about hardware hacking for Hackaday (www.hackaday.com), but talk about other topics here.