Unary operator expected in Bash

Publish date: 2024-07-07

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 
2

2 Answers

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 
3

It looks good to me. Are you sure you've set BLOCK1FRAN correctly?

whatever() { echo "it works"; } foo=42 BLOCK1FRAN=foo if [ $(date +%k%M) -ge ${!BLOCK1FRAN} ] ; then whatever ; fi it works 

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoaWtvaW2AdH%2BOrqWaqqlivLGx0ZqrqKpdmsWxscKtnJ1lmaN6o63SoQ%3D%3D