Hello I have a simple example which I am confused about:
#!bin/bash
#test_str is a script to test whether a string is empty or not
x=''
if [ -n $x ]
then
echo "'$x' is not empty"
else
echo "'$x' empty"
fi
chmod u+x test_str
./test_str
The output:
'' is not empty
This occurs also if I've not declared a variable (x here).
If I use the flag
-z
to test for emptiness it works fine:if [ -z $x ] then echo "'$x' is empty" else echo "'$x' not empty" fi
The output:
'' is empty
- So how can I use
-n
correctly? Thank you!