#!/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 "" ]]
.
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.
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 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.
Test string equality with =
.
#!/bin/bash
export PROCNAME=test
export TABLE_ID=0
if [ "${TABLE_ID}" = "" ]; then
echo hello
fi
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With