Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

This question already has an answer here:

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
share|improve this question

marked as duplicate by jasonwryan, jimmij, Archemar, muru, Gilles yesterday

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers 2

up vote 7 down vote accepted

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

if [ -z "$var" ] && [ -z "$non" ]
share|improve this answer
    
Thanks Andy for the answer! –  JavaQuest yesterday

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.

share|improve this answer
    
Thanks @primero for the explanation –  JavaQuest yesterday

Not the answer you're looking for? Browse other questions tagged or ask your own question.