Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[: -eq: unary operator expected

Tags:

bash

aix

#!/bin/bash    
export PROCNAME=test
export TABLE_ID=0

if [ ${TABLE_ID} -eq "" ]; then
        echo hello
fi

above throws error:

[: -eq: unary operator expected

How to fix this with out double square brackets [[ ${TABLE_ID} -eq "" ]].

like image 948
user2711819 Avatar asked Jan 30 '15 15:01

user2711819


People also ask

What is unary operator expected?

The word unary is basically synonymous with “single.” In the context of mathematics, this could be a single number or other component of an equation. So, when Bash says that it is expecting a unary operator, it is just saying that you are missing a number in the script.

How check if variable is empty bash?

To find out if a bash variable is empty: Return true if a bash variable is unset or set to the empty string: if [ -z "$var" ]; Another option: [ -z "$var" ] && echo "Empty" Determine if a bash variable is empty: [[ ! -z "$var" ]] && echo "Not empty" || echo "Empty"

How do I put sleep in a bash script?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.


2 Answers

Test string equality with =.

#!/bin/bash    
export PROCNAME=test
export TABLE_ID=0

if [ "${TABLE_ID}" = "" ]; then
    echo hello
fi
like image 164
Mars Avatar answered Sep 28 '22 03:09

Mars


You can use -z to test if a variable is empty:

if [ -z "$variable" ]; then
   ...
fi

From man test:

-z STRING
        the length of STRING is zero

See an example:

$ r="roar"
$ [ -z "$r" ] && echo "empty" || echo "not empty"
not empty
$ r=""
$ [ -z "$r" ] && echo "empty" || echo "not empty"
empty
like image 40
fedorqui 'SO stop harming' Avatar answered Sep 28 '22 03:09

fedorqui 'SO stop harming'