Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 shell-script Sep 14 '15 at 21:17

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.

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 Sep 14 '15 at 17:50

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 Sep 14 '15 at 17:52

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