Like this (don't use anymore the outdated expr
):
dayleft=$(( arithmetic expression ))
If you need float numbers in bash, use bc instead:
dayleft=$(bc -l <<< "scale=2; 100/3")
As stated by Stéphane Chazelas in comments, ksh93
, zsh
and yash
do support floating points within $((...))
and ((...))
.
expr
is a program used in ancient shell code to do math. In Posix shells like bash, use $(( expression ))
. In bash, ksh88+, mksh/pdksh, or zsh, you can also use (( expression ))
or let expression
((...))
is an arithmetic command, which returns an exit status of 0 if the expression is nonzero, or 1 if the expression is zero. Also used as a synonym for "let", if side effects (assignments) are needed. See http://mywiki.wooledge.org/ArithmeticExpression
$((...))
is an arithmetic substitution. After doing the arithmetic, the whole thing is replaced by the value of the expression. See http://mywiki.wooledge.org/ArithmeticExpression
Command Substitution: "$(cmd "foo bar")"
causes the command 'cmd' to be executed with the argument 'foo bar' and "$(..)"
will be replaced by the output. See http://mywiki.wooledge.org/BashFAQ/002 and http://mywiki.wooledge.org/CommandSubstitution
Avoid using UPPER CASE variables, they are reserved for system use
Finally
expiryepoch=$(date --date="$expirydate" +%s)
todayepoch=$(date --date="$todaysdate" +%s)
dayleft=$(bc <<< "scale=2; (todayepoch - expiryepoch) / 86400")