help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Can Bash do simple math?


From: bash
Subject: Can Bash do simple math?
Date: Mon, 5 Aug 2024 23:42:44 +0200

I have a somewhat strange issue.
In a script I use the `((VAR = expression ))` syntax to calculate a
simple remainder by division:

  ((DIFF = $2 - $1))
  ((SECONDS = DIFF % 60))
  ((MINUTES = (DIFF % 3600) / 60))
  ((HOURS = DIFF / 3600))

  echo "$HOURS:$MINUTES:$SECONDS"

However, it seems as if Bash sometimes miscalculates the remainder and
prints an errorneous result string.

I have written a small example and had some friends run it to confirm
this issue is not only on my machine.
I highly doubt Bash _sometimes_ miscalculates basic math and neither
me or my friends can spot any errors in my code. Am I stupid? What am
I missing? 

The minimal example code to produce an error:
https://gist.github.com/blaklinten/3dd29f3f73b75337a86b8d7cdb59bdd7

I am eager to hear thoughts on this :)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]