What does -z flag mean in shell script used in if condition



Assuming that you mean something like this:
Code:
if [[ -z $SOMEVARIABLE ]]; then
    echo "Do something here .... "

Or this:
Code:
if [ -z $SOMEVARIABLE ]; then
    echo "Do something here .... "

In both cases, the -z flag is a parameter to the bash's "test" built-in (a built-in is a command that is built-into the shell, it is not an external command). The -z flag causes test to check whether a string is empty. Returns true if the string is empty, false if it contains something.

NOTE: The -z flag doesn't directly have anything to do with the "if" statement. The if statement is used to check the value returned by test. The -z flag is part of the "test" command.

The previous examples could also have been written like this:
Code:
if test -z $SOMEVARIABLE

Any expressions enclosed in square brackets, or double square brackets [ ] and [[ ]] are equivalent to using the "test" command. Using either version with the square brackets looks tidier than using test directly, it makes the code a bit more readable/understandable.

If I remember rightly, there is a slight difference between using single square brackets [ ] and double [[ ]] square brackets too.

I think the single bracket version of test is portable to other shells and supports a limited, standard set of flags/operations, whereas the double bracket version is specifically for bash's test built-in which supports some extra, non-standard operations.

To see further help/information for the test built-in, use the command:
Code:
help test
 
... and that's a good reason, isn't it Jas, for not naming a file you are testing as "test"?

For example, you would not create an empty file

Code:
touch test

Better would be

Code:
touch testfile

#or better

touch checkthis

Calling a file "test" could cause conflict with the command.

Welcome @shraddha verma to linux.org, hope you enjoy your stay here :p

Wizard
 
... and that's a good reason, isn't it Jas, for not naming a file you are testing as "test"?

For example, you would not create an empty file

Code:
touch test

Better would be

Code:
touch testfile

#or better

touch checkthis

Calling a file "test" could cause conflict with the command.

Welcome @shraddha verma to linux.org, hope you enjoy your stay here :p

Wizard

Actually, because test is a built-in, it shouldn't matter. But I guess it might affect you if you put a script, or executable called 'test' somewhere in your path.

However, from a quick test on my laptop - I just put a simple script called test into my personal bin directory (~/bin/).
Using the command:
Code:
which test
my system displays that ~/bin/test would be used.

I then tried running some of my other scripts which use test and all ran normally. So the shell appears to be using the normal, built-in 'test'. It isn't calling my 'test' script.

Also, after removing the 'test' script from ~/bin, the which command displays that /usr/bin/test is the test executable.....Hmmm.. I always thought that it was a built-in, but it's also an external command/tool... Meh - whatever! Doesn't seem to change the usage of it!

Just a BTW - double brackets support shell globbing, I've been reading an article here

https://linuxacademy.com/blog/linux/conditions-in-bash-scripting-if-statements/

... that illustrates about 5 differences between single and double, it's 2012, and Jas could say if it is still current.

Cheers

Wizard

Yes, that article is definitely still correct and relevant! I'd just forgotten the exact reasons/differences for the single and double square brackets!
 

Members online


Latest posts

Top