5

This question already has an answer here:

My shell is bash and I have three variables x=5; y=7; z=7.5

I am trying to use the below statement to test the equality of variables $y and $z as shown below

Command: test $z -eq $y; echo $?

But, it shows the following error and I am not able to resolve it.

-bash: test: 7.2: integer expression expected

2

Note: when I apply the same test command on integers $x and $y, they work fine.

marked as duplicate by Gilles, GAD3R, Jeff Schaller, Stephen Harris, Thomas Dickey Sep 6 '16 at 0:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • i am not sure how to apply "bc in this case – AngiSen Sep 4 '16 at 15:00
  • Can't you use string comparison "=" for your use case? Float comparison for equality is usually not very useful. – rudimeier Sep 4 '16 at 15:47
5

You can use the == operator of bc.

test "$(echo $x == $z |bc)" -eq 1

expr1 == expr2
The result is 1 if expr1 is equal to expr2.

-- from man bc(1)

Not the answer you're looking for? Browse other questions tagged or ask your own question.