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
Related videos on Youtube
Comments
-
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
-
alvitsYou are using indirection. If the variable
${BLOCK1FRAN}
points to an empty variable, you'll get the error message.
-
-
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 about 10 yearsDouble 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 about 10 yearsThe 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 seebash
manpage.