0

I have written a arithmetic operation in a file, below is my input. I'm trying to assign the result into a variable by

c=`cat file`

c=`echo cat file`

But is not working, What's the correct way to do it?

cat file
$(($a+$b))

Code Snippet:

a=40.3
b=7.4
c='cat file`
Marjer
  • 1,313
  • 6
  • 20
  • 31

1 Answers1

2

Can I assume that you've created weird artificial requirements? The file in its current form isn't usable...

If you had c=$(($a+$b)) in the file, you could simply source it:

. ./file
echo $c    # 47

You could also have a script that expects two arguments, echo $(($1+$2)), and you could use it like this:

c=$(./file $a $b)
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • I Have a huge number of variables and it's difficult to pass the argumnent. But `. ./file` is rounding the values, updated the input with float values `echo $c` `#47` i want the result as 47.7 – Marjer Nov 21 '14 at 20:48
  • still prefer to pass them. that's how you can build bigger programs. none of the solutions rounding, *your* approach does. see: http://stackoverflow.com/questions/6348902/how-can-i-add-numbers-in-a-bash-script – Karoly Horvath Nov 21 '14 at 20:53