5

I am trying to run a small script which check for two variables to see if they are empty or not. I am getting the correct output but if also shows me error for missing right parenthesis. I tried using double parenthesis as well as round bracket but didn't work.

var=""
non="hi"

 if ([ -z "$var"] && [ -z "$non"])
then
    echo "both empty"

else
    echo "has data"
fi

OUTPUT:

line 6: [: missing `]'
has data
muru
  • 72,889
JavaQuest
  • 207

2 Answers2

12

You need a space bettween "$non" and ], and you don't need ()'s:

if [ -z "$var" ] && [ -z "$non" ]
Archemar
  • 31,554
Andy Dalton
  • 13,993
4

As Andy Dalton said in his answer you need a space between "$non" and the closing square parentheses ] like this:

[ -z "$non" ]

I just want to add a bit of motivation why this is needed. In bash the square parentheses are commands (you can try man [ to see, it's the manual for the test command), so you need the space for bash to figure out which command you are invoking.

primero
  • 590