The code for the script is:
echo "Years:" read age x=`expr $age*365` echo -e $x
The output when I call this script from the command line is as follows:
(say I put 20)
Why is it not evaluating?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
expr is archaic and largely obsolete these days. It's also very limited and clumsy to use compared to better alternatives like:
bash has integer-only arithmetic built-in these days.
$ age=20 $ echo "$((age * 365))" 7300 $ x=$((age*365)) $ echo "$x" 7300
bc for floating-point arithmetic:
$ age=20 $ echo "$age * 365.25" | bc 7305.00
Or, if you want to drop the decimal fraction:
$ echo "scale=0; $age * 365.25/1" | bc 7305
The division by 1 is needed because addition, subtraction, and multiplication ignore the
scale setting in
bc. This is a long-standing annoyance and is unlikely to be "fixed".
if you prefer RPN, you could use
$ age=20 $ dc -e "$age 365.25 * p" 7305.00
Note: backticks are obsolete and deprecated too. Use
$(...) instead of backticks. e.g.
x=$(echo "$age * 365.25" | bc) x=$(dc -e "$age 365.25 * p")