facebook youtube pinterest twitter reddit whatsapp instagram

Check If Variable Is Not Empty [isset] (Bash)

In the getting started with bash guide, we took a sneak peek in customizing script with argument, the issue with the example is that we aren't checking if the variable is set or not before returning the output, which we are going to amend in this guide.

We would ensure that a value has been supplied to the first positional parameter.

Take for example...

#!/bin/bash
echo "Hello $1"
exit 0

If you run the following script, it would output Hello, and if you pass a first positional parameter, it would output "Hello" with whatever parameter you pass to the variable.

But we can do better by first checking if the value we expected has been supplied, if it is, then we echo the result, if not, we skip the hello statement entirely. This is the modified version:

#!/bin/bash
test -z $1 || echo "Hello $1"
exit 0

The test statement is used for file comparison, it returns a status of 0 (true) or 1 (false) depending on the evaluation of the conditional expression EXPR. Each part of the expression must be a separate argument.

So, test -z $1 would check if the $1 is empty, and if it is, you won't get any output, if otherwise, we would see the hello message with the string we supply.

If you store the name of the script isset.sh, and you run isset.sh Devsrealm, you would get the following:

user@server:~/bin$ isset.sh Devsrealm
Hello Devsrealm

In the coming guide, we would take a closer look at the test shell builtin command.

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Returning Values From Functions in (Bash)

    In the guide function in bash, we dive deep in bash functions and we also pass some information into the functions to carry out a certain operation. In this guide, I'll show you how to get informatio

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f