-1

I have created a file that contains arithmetic expressions, one per line. How can I evaluate the expressions and display the results, from the command line?

The file looks like this:

1 + 2
6 * 4
97 % 12
43215 / 43 * 100

The goal is to output each result, in order, also one per line. I recall there is a way to do this that only requires one command, and that command used might start with w, but I can't remember how.

  • Welcome to AskUbuntu! I hate to say "google it", but this question could have been answered with a quick google of "bash math". See how to ask for more info. – wjandrea Sep 12 '17 at 18:26
  • 3
    This was closed as "not about Ubuntu." I don't see anything in the help page about our scope or Ask Ubuntu Meta to support that. It seems at least as on-topic as most other simple terminal-based tasks, and we have this and that, which are different but related. The close reason strongly suggests it wasn't closed "as homework," which I don't think we do anyway, plus I think my edit fixes that and the quality problems. The answer is for Ubuntu and well-received. – Eliah Kagan Sep 13 '17 at 7:14
  • Though clearly related, does Any command line calculator for Ubuntu? really answer this? Both the question there and all its answers are entirely about interactive use. The task asked about here--running a single command to noninteractively simplify arithmetic expressions from a file--is not covered there. – Eliah Kagan Sep 14 '17 at 18:31
10

You can use bc as calculator, and send your file to it as input:

$ cat maths.txt
1 + 2
6 * 4
97 % 12
43215 / 43 * 100

$ bc < maths.txt
3
24
1
100500
| improve this answer | |

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