12
#!/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 "" ]].

fedorqui
  • 275,237
  • 103
  • 548
  • 598
user2711819
  • 960
  • 3
  • 16
  • 29

3 Answers3

8

Test string equality with =.

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

if [ "${TABLE_ID}" = "" ]; then
    echo hello
fi
Mars
  • 4,677
  • 8
  • 43
  • 65
4

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
fedorqui
  • 275,237
  • 103
  • 548
  • 598
1
#!/bin/bash    
export PROCNAME=test
export TABLE_ID=0

[ -z ${TABLE_ID} ] && echo hello