| Tuesday May 24th 2016

Writing Robust Bash Shell Scripts

Here’s a decent writeup on how to tweak your bash scripts to be more reliable. It goes step by step and is easy to understand even for a novice. Cheers!

Many people hack together shell scripts quickly to do simple tasks, but these soon take on a life of their own. Unfortunately shell scripts are full of subtle effects which result in scripts failing in unusual ways. It’s possible to write scripts which minimize these problems. In this article, I explain several techniques for writing robust bash scripts.

Be prepared for spaces in filenames
Someone will always use spaces in filenames or command line arguments and you should keep this in mind when writing shell scripts. In particular you should use quotes around variables.

if [ $filename = "foo" ];

will fail if $filename contains a space… This can be fixed by using:

if [ "$filename" = "foo" ];

Read more…

Related Posts: On this day...

Leave a Reply

You must be logged in to post a comment.