3

I'm learning Shell scripting, I'm trying to write a small script that adds and multi number as shown below But amt value not display

value=212
amt=`expr "( $value * 2 + ( $value * 2 * .075 ) ) " | bc`
echo $amt
Jahid
  • 21,542
  • 10
  • 90
  • 108
Ban
  • 339
  • 1
  • 5
  • 16
  • possible duplicate of [How can I add numbers in a bash script](http://stackoverflow.com/questions/6348902/how-can-i-add-numbers-in-a-bash-script) – that other guy Jul 13 '15 at 18:16
  • 1
    That code works fine: http://ideone.com/MauwYb though I wouldn't use `expr` there since it isn't doing anything that `echo` can't do and is deprecated. – Etan Reisner Jul 13 '15 at 18:18

2 Answers2

1

Your code works fine, but I suggest some improvements:

  1. Use $(...) instead of backticks.
  2. Replace expr with echo.

Example:

value=212
amt=$(echo "( $value * 2 + ( $value * 2 * .075 ) ) " | bc)
echo $amt

Output:

455.800
ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
Jahid
  • 21,542
  • 10
  • 90
  • 108
-1

First things first. This:

value * 2 + (value * 2 * .075)

is a hot mess, this is equivalent:

value * 43 / 20

Next, I prefer to use awk for this job:

#!/usr/bin/awk -f
BEGIN {
  value = 212
  print value * 43 / 20
}
Zombo
  • 1
  • 62
  • 391
  • 407