
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 different. I found a wrapper that tries to figure it out, but it was broken for KDialog so I forked it and fixed it.
4. Compile
Sometimes you want to give someone a shell script but you don’t want them poking around in it and making changes. It would be nice to compile shell scripts but not very feasible in the technical sense of the word compile. What you can do, as I figured out on Unix decades ago, is encrypt your shell script and have a little program that decrypts the script and feeds it to a shell over a pipe. Very old code and I’ve updated it a decade ago for Linux. The encryption is not very strong. It is only meant to keep prying eyes out of your code, not thwart international spies!
Look for some more bash scripting tips soon and don’t forget to check out my One Minute Bash Tips like the one about script execution exclusion or the one that shows how to make a script not run immediately when the system is just starting.


