This question already has an answer here:
I don't set any values for $pass_tc11; so it is returning null while echoing. How to compare it in if
clause?
Here is my code. I don't want "Hi" to be printed...
-bash-3.00$ echo $pass_tc11
-bash-3.00$ if [ "pass_tc11" != "" ]; then
> echo "hi"
> fi
hi
-bash-3.00$
First of all, note you are not using the variable correctly:
Anyway, to check if a variable is empty or not you can use
-z
--> the string is empty:or
-n
--> the length is non-zero:From
man test
:Samples:
fedorqui has a working solution but there is another way to do the same thing.
Chock if a variable is set
Or to verify that a variable is empty
tldp.org has good documentation about if in bash:
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html