0

I would like to check if variable is empty or not. I have found several solutions but it doesn't work for me. I am using this code:

#!/bin/bash
    
if [[ -z "$test" ]]
then
   echo not empty
   pwd
else
   echo empty
fi

For not empty variable, I get this

pc@ubuntu:~$ echo $test
sfdgsdfg asdf dfgf
pc@ubuntu:~$ ./if_test.sh
not empty
pc@ubuntu:~$ 

However, if I have empty variable, I get also message that it is not empty.

pc@ubuntu:~$ echo $test

pc@ubuntu:~$ 
pc@ubuntu:~$ ./if_test.sh
not empty
pc@ubuntu:~$ 

Could someone help me, where is the problem?

polik989
  • 11
  • 2
  • the test variable that you set is not inherited by your child process. if_test.sh doesn't see the test variable you set in bash. use `export test=...` – Shloim Jul 28 '20 at 13:26
  • If I make export test, than I get empty, even tough it is not empty... – polik989 Jul 28 '20 at 13:31

0 Answers0