# Shell arithmetic : operations on decimal points

**URL:** <https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567>\
**Category:** Shell Programming and Scripting\
**Created:** [April 9, 2014, 5:35am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567 "2014-04-09T05:35:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rishifrnds](https://community.unix.com/letter_avatar/rishifrnds/32/6_449c533f586939b6e5f246632a46421f.png) [@rishifrnds](https://community.unix.com/u/rishifrnds)\
**Post date:** [April 9, 2014, 5:35am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/1 "2014-04-09T05:35:52Z")

</div>

i am having a varialbe a , which is input to my file  
i want to multiply this input with value .43, and assign it to variable b.  
i tried it as below:

```nohighlight
#!/bin/sh
a=$1
b=`expr $1\*0.43`
echo b=$b

error : expr: non-integer argument

```

Please tell me , how to do this.

Thanks

---

<div class="post-metadata">

**Author:** ![Klashxx](https://community.unix.com/user_avatar/community.unix.com/klashxx/32/326_2.png) [@Klashxx](https://community.unix.com/u/Klashxx)\
**Post date:** [April 9, 2014, 6:00am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/2 "2014-04-09T06:00:01Z")

</div>

This way:

```nohighlight
#!/bin/sh

a=$1
b=$(echo "$a * 0.43"|bc)
echo $b

```

---

<div class="post-metadata">

**Author:** ![rishifrnds](https://community.unix.com/letter_avatar/rishifrnds/32/6_449c533f586939b6e5f246632a46421f.png) [@rishifrnds](https://community.unix.com/u/rishifrnds)\
**Post date:** [April 9, 2014, 6:22am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/3 "2014-04-09T06:22:28Z")

</div>

Hi Klashxx,

if i try that code, it gives error:

```nohighlight
./test: syntax error at line 5: `b=$' unexpected

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [April 9, 2014, 6:25am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/4 "2014-04-09T06:25:08Z")

</div>

If you're using an old SH (probably on Solaris), you might have to revert to using backquotes, as in your original code

```nohighlight
b=`...`

```

You can also use printf in place of bc

```nohighlight
b=`printf "%.2f" $1*0.43`

```

---

<div class="post-metadata">

**Author:** ![rishifrnds](https://community.unix.com/letter_avatar/rishifrnds/32/6_449c533f586939b6e5f246632a46421f.png) [@rishifrnds](https://community.unix.com/u/rishifrnds)\
**Post date:** [April 9, 2014, 6:27am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/5 "2014-04-09T06:27:12Z")

</div>

Hi Scott,

It would b great if u correct this code itself and show me:

```nohighlight
#!/bin/sh a=$1 b=$(echo "$a * 0.43"|bc) echo $b

```

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [April 9, 2014, 6:28am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/6 "2014-04-09T06:28:26Z")

</div>

I already did, or so I thought. Do you have all that code on one line? If so, why? If not, please post it as it is in your script. We don't need to be second guessing exactly what you have written.

It would be nice if you used Code Tags so I wouldn't have to spend my time adding them for you.

---

<div class="post-metadata">

**Author:** ![rishifrnds](https://community.unix.com/letter_avatar/rishifrnds/32/6_449c533f586939b6e5f246632a46421f.png) [@rishifrnds](https://community.unix.com/u/rishifrnds)\
**Post date:** [April 9, 2014, 6:34am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/7 "2014-04-09T06:34:23Z")

</div>

#!/bin/sh

```nohighlight
a=$1
b=$(echo "$a * 0.43"|bc)
echo $b

```

---------- Post updated at 04:04 PM ---------- Previous update was at 04:00 PM ----------

```nohighlight
#!/bin/sh

a=$1
b=`printf "%.2f" $1*0.43`
##b=$(echo "$a * 0.43"|bc)
echo $b

```

Result:

```nohighlight
printf: 10*0.43: value not completely converted
10.00

```

expected ans 4.3

---

<div class="post-metadata">

**Author:** ![Scott](https://community.unix.com/letter_avatar/scott/32/6_449c533f586939b6e5f246632a46421f.png) [@Scott](https://community.unix.com/u/Scott)\
**Post date:** [April 9, 2014, 6:49am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/8 "2014-04-09T06:49:37Z")

</div>

But did you try Klashxx's solution with backquotes?

```nohighlight
#!/bin/sh

a=$1
b=`echo "$a * 0.43"|bc`
echo $b

```

---

<div class="post-metadata">

**Author:** ![rishifrnds](https://community.unix.com/letter_avatar/rishifrnds/32/6_449c533f586939b6e5f246632a46421f.png) [@rishifrnds](https://community.unix.com/u/rishifrnds)\
**Post date:** [April 9, 2014, 7:25am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/9 "2014-04-09T07:25:32Z")

</div>

hey,

It worked 😃

Thank you very much Klashxx and Scott

---

<div class="post-metadata">

**Author:** ![jethrow](https://community.unix.com/letter_avatar/jethrow/32/6_449c533f586939b6e5f246632a46421f.png) [@jethrow](https://community.unix.com/u/jethrow)\
**Post date:** [April 9, 2014, 11:04pm UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/10 "2014-04-09T23:04:22Z")

</div>

... could utilize awk, which natively handles floats:

```nohighlight
b=`echo $1|awk '{print $0*.43}'`

```

... my bad, missed the second page where this had been resolved ...

---

<div class="post-metadata">

**Author:** ![rishifrnds](https://community.unix.com/letter_avatar/rishifrnds/32/6_449c533f586939b6e5f246632a46421f.png) [@rishifrnds](https://community.unix.com/u/rishifrnds)\
**Post date:** [April 10, 2014, 6:41am UTC](https://community.unix.com/t/shell-arithmetic-operations-on-decimal-points/341567/11 "2014-04-10T06:41:47Z")

</div>

Thanks Jethrow, ur suggestion works 😃

```nohighlight
#!/bin/sh

a=$1
c=$2
##b=`printf "%.2f" $1*0.43`
b=`echo $1|awk '{print $0*.43}'`
##b=`echo "$a * $c"|bc`
echo $b

```
