How to handle user variable in a shell/bash script

Hello ! I’m Xavier Jouvenot and in this small post, we are going to see how to handle user variable in a shell/bash script.

Self promotion: You can find other articles on my website 😉

Problematic

When creating some shell scripts, you may want to have some parameters accessible to the user, so that he could either pass some inputs to your script, or specify some elements for which you will have set some default value.

There are several ways to do it, but we are going to see the simplest and most powerful solution to me. Hopefully, you will find it convenient too 😉

Solution

Let’s dive right in the shell script and see what defining a user variable looks like:

Even if it only take one line, a lot of things happen here. Let’s take a little time to explain exactly what 🙂 In the first part of this line, [ -n "$VARIABLE" ], we check if VARIABLE is not empty. Then, with the operator|| we indicate that we want to do something if the previous condition is false, which means we want to do something if VARIABLE is not not empty, or, if you remove the double negation for clarity, wa want to do something is the variable is empty. And finally, we declare want we want to do in that case, which is to set VARIABLE to the default value that we specified. 😉

Now, all we need is to pass a value to the variable like that:

And voilà now, how to to handle user variable in a shell/bash script 😉

Thank you all for reading this article, And until my next article, have a splendid day 😉

Interesting links

Originally published at http://10xlearner.com on February 10, 2021.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store