( test -n $st ) != ( test -z $st ) right? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of ( test -n $st ) != ( test -z $st ) right? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

As I read,

test -n $string ==> Exit status is 0 if $string is not null, 1 otherwise

and

test -z $string ==> Exit status is 0 if $string is null, 1 otherwise

But in this particular example, (I tried to create a null string) it seems I am missing some thing.

#!/bin/sh
str=""
test -n $str
echo $?
test -z $str
echo $?

Output of this is:

0
0

enter image description here

Can anyone give an explanation for this strange behavior??

Answer

Put $str inside double quotes!

The -n test requires that the string be quoted within the test brackets. Using an unquoted string with ! -z, or even just the unquoted string alone within test brackets (see Example 7-6) normally works, however, this is an unsafe practice. Always quote a tested string. Other Comparison Operators

We are here to answer your question about ( test -n $st ) != ( test -z $st ) right? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji