Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

How do i multiply 2 array index variables given the following

foo=(1 2)
bar=(0.1 0.2)

foobar=$((foo[1]*price[1])) # this is wrong

echo "$foobar"

Current output : 0

Correct/Expected output : 0.4

share|improve this question

closed as off-topic by roadmr, dobey, Aditya, Radu Rădeanu, falconer Jan 10 '14 at 16:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – roadmr, dobey, Aditya, Radu Rădeanu, falconer
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

Aside from the typo (you have used price instead of bar) the main issue is that the shell only performs integer arithmetic. If you want to perform floating-point arithmetic, the easiest way is probably via the bc command e.g.

foobar=$(bc <<< ${foo[1]}*${bar[1]})

for example

$ foo=(1 2); bar=(0.1 0.2)
$ foobar=$(bc <<< "${foo[1]}*${bar[1]}")
$ echo $foobar
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.