Unary operator expected in Bash

12,142

You are using indirection. If the variable ${BLOCK1FRAN} points to an empty variable, you'll get the error message. Make sure that the variable pointed by ${BLOCK1FRAN} contains a valid numeric value.

If you want an empty string and nonnumeric values to be evaluated as zero (0), use the following syntax.

if [[ $(date +%k%M) -ge ${!BLOCK1FRAN} ]]; then whatever ; fi
Share:
12,142

Related videos on Youtube

Paolo
Author by

Paolo

Terrible hobby programmer, trying to learn something.

Updated on June 04, 2022

Comments

  • Paolo
    Paolo almost 2 years

    I've seen questions regarding the same issue, but all of them are about strings. How about integers? Why am I getting the "unary operator expected" error?

     if [ $(date +%k%M) -ge ${!BLOCK1FRAN} ] ; then whatever ; fi
    
    • alvits
      alvits
      You are using indirection. If the variable ${BLOCK1FRAN} points to an empty variable, you'll get the error message.
  • kojiro
    kojiro about 10 years
    +1 Another approach: Declare names in bash you expect to be numeric to have the integer attribute. This gives a semblance of indirection, but also guarantees the value will be numeric. declare -i x BLOCK1FRAN; BLOCK1FRAN=x; echo $BLOCK1FRAN
  • Paolo
    Paolo about 10 years
    Double brackets instead of singles ones seem to work well, thank you. May I ask why this is? And why would anyone choose to use [ instad of [[?
  • alvits
    alvits about 10 years
    The main difference between [ and ] and [[ and ]] is that word splitting and pathname expansion are not performed on the latter. For example, if you have * within [ and ], it will be expanded to the files in the $PWD whereas the same * inside [[ and ]] won't. And most likely you intend to use the literal *. For more details see bash manpage.

Related