Check if bash variable equals 0

BashEqualsZero

Bash Problem Overview


I have a bash variable depth and I would like to test if it equals 0. In case yes, I want to stop executing of script. So far I have:

zero=0;

if [ $depth -eq $zero ]; then
	echo "false";
	exit;
fi

Unfortunately, this leads to:

 [: -eq: unary operator expected

(might be a bit inaccurate due to translation)

Please, how can I modify my script to get it working?

Bash Solutions


Solution 1 - Bash

Looks like your depth variable is unset. This means that the expression [ $depth -eq $zero ] becomes [ -eq 0 ] after bash substitutes the values of the variables into the expression. The problem here is that the -eq operator is incorrectly used as an operator with only one argument (the zero), but it requires two arguments. That is why you get the unary operator error message.

EDIT: As Doktor J mentioned in his comment to this answer, a safe way to avoid problems with unset variables in checks is to enclose the variables in "". See his comment for the explanation.

if [ "$depth" -eq "0" ]; then
   echo "false";
   exit;
fi

An unset variable used with the [ command appears empty to bash. You can verify this using the below tests which all evaluate to true because xyz is either empty or unset:

  • if [ -z ] ; then echo "true"; else echo "false"; fi
  • xyz=""; if [ -z "$xyz" ] ; then echo "true"; else echo "false"; fi
  • unset xyz; if [ -z "$xyz" ] ; then echo "true"; else echo "false"; fi

Solution 2 - Bash

Double parenthesis (( ... )) is used for arithmetic operations.

Double square brackets [[ ... ]] can be used to compare and examine numbers (only integers are supported), with the following operators:

· NUM1 -eq NUM2 returns true if NUM1 and NUM2 are numerically equal.

· NUM1 -ne NUM2 returns true if NUM1 and NUM2 are not numerically equal.

· NUM1 -gt NUM2 returns true if NUM1 is greater than NUM2.

· NUM1 -ge NUM2 returns true if NUM1 is greater than or equal to NUM2.

· NUM1 -lt NUM2 returns true if NUM1 is less than NUM2.

· NUM1 -le NUM2 returns true if NUM1 is less than or equal to NUM2.

For example

if [[ $age > 21 ]] # bad, > is a string comparison operator

if [ $age > 21 ] # bad, > is a redirection operator

if [[ $age -gt 21 ]] # okay, but fails if $age is not numeric

if (( $age > 21 )) # best, $ on age is optional

Solution 3 - Bash

Try:

zero=0;

if [[ $depth -eq $zero ]]; then
  echo "false";
  exit;
fi

Solution 4 - Bash

you can also use this format and use comparison operators like '==' '<='

  if (( $total == 0 )); then
      echo "No results for ${1}"
      return
  fi

Solution 5 - Bash

Specifically: ((depth)). By example, the following prints 1.

declare -i x=0
((x)) && echo $x

x=1
((x)) && echo $x

Solution 6 - Bash

You can try this:

: ${depth?"Error Message"} ## when your depth variable is not even declared or is unset.

NOTE: Here it's just ? after depth.

or

: ${depth:?"Error Message"} ## when your depth variable is declared but is null like: "depth=". 

NOTE: Here it's :? after depth.

Here if the variable depth is found null it will print the error message and then exit.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPerlnikaView Question on Stackoverflow
Solution 1 - BashcyonView Answer on Stackoverflow
Solution 2 - BashRaviteja GubbaView Answer on Stackoverflow
Solution 3 - BashJacek DominiakView Answer on Stackoverflow
Solution 4 - BashGurubaranView Answer on Stackoverflow
Solution 5 - BashStephen NiedzielskiView Answer on Stackoverflow
Solution 6 - BashPrabhat Kumar SinghView Answer on Stackoverflow