• Using echo “20+5” literally produces the text “20+5”.
  • What command you use to get the numeric sum, 25 in this case?
  • Also, what’s the easiest way to do it just using bash for floating point? For example, echo $((3224/3807.0)) prints 0 :(.
  • You are looking for answers using either the basic command shell (‘command line’) itself or through using languages that are available from the command line.

You could use bc. E.g.,

[pastacode lang=”bash” manual=”%24%20echo%20%2225%20%2B%205%22%20%7C%20bc%0A30%0A” message=”Bash Code” highlight=”” provider=”manual”/]

Alternatively bc <<< 25+5 will also work.

Or interactively, if you want to do more than just a single simple calculation:

[pastacode lang=”bash” manual=”%24%20bc%0Abc%201.06.95%0ACopyright%201991-1994%2C%201997%2C%201998%2C%202000%2C%202004%2C%202006%20Free%20Software%20Foundation%2C%20Inc.%0AThis%20is%20free%20software%20with%20ABSOLUTELY%20NO%20WARRANTY.%0AFor%20details%20type%20%60warranty’.%20%0A25%20%2B%205%0A30%0A” message=”Bash Code” highlight=”” provider=”manual”/]

bc -q will jump into bc right away without the header/copyright info. For more information see the bc man page

This page also shows how to set up an alias or function to do calculation like this:

  •  get the result
[pastacode lang=”bash” manual=”c%20%2225%20%2B%205%22%20%0A” message=”Bash Code” highlight=”” provider=”manual”/] [ad type=”banner”]

You can use calc:

If you just enter calc with no other arguments it enters an interactive mode where you can just keep doing math.

You exit this by typing exit:

[pastacode lang=”bash” manual=”C-style%20arbitrary%20precision%20calculator%20(version%202.12.3.3)%0ACalc%20is%20open%20software.%20For%20license%20details%20type%3A%20%20help%20copyright%0A%5BType%20%22exit%22%20to%20exit%2C%20or%20%22help%22%20for%20help.%5D%0A%3B%202%2B4%0A6%0A%3B%203%2B5%0A8%0A%3B%203.4%2B5%0A8.4%0A%3B%202%5E4%0A16%0A%3B%20exit%0A” message=”Bash Code” highlight=”” provider=”manual”/]

Or you use it with the expression as an argument and it will provide the answer and then exit

[pastacode lang=”bash” manual=”%24calc%202%20%2B%204%0A%20%20%20%206%0A%24%0A” message=”Bash Code” highlight=”” provider=”manual”/]

calc is similar to bc

There are many ways to calculate.

For simple expressions you can use bash itself:

[pastacode lang=”bash” manual=”echo%20%24((20%2B5))” message=”Bash Code” highlight=”” provider=”manual”/]

or expr:

[pastacode lang=”bash” manual=”expr%2020%20%2B%205%0A” message=”BAsh Code” highlight=”” provider=”manual”/] [ad type=”banner”]

And for complex cases there is great tool bc:

[pastacode lang=”bash” manual=”%20%20%20%20echo%20%2220%2B5%22%20%7C%20bc” message=”Bash Code” highlight=”” provider=”manual”/]

bc can calculate even very complex expression with roots, logarithms, cos, sin and so on.

Integer Math:

Performing Math Calculation in Bash:

First way to do math with integer (and only integer) is to use the command “expr — evaluate expression”.

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20expr%201%20%2B%201%0A2%0AMac-n-Cheese%3A~%20nicolas%24%20myvar%3D%24(expr%201%20%2B%201)%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A2%0AMac-n-Cheese%3A~%20nicolas%24%20expr%20%24myvar%20%2B%201%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20expr%20%24myvar%20%2F%203%0A1%0AMac-n-Cheese%3A~%20nicolas%24%20expr%20%24myvar%20%5C*%203%0A9%0A” message=”Bash Code” highlight=”” provider=”manual”/]

When doing a “multiply by” make sure to backslash the “asterisk” as it’s a wildcard in Bash used for expansion.

Another alternative to expr, is to use the bash builtin command let.

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A6%0AMac-n-Cheese%3A~%20nicolas%24%20let%20myvar%2B%3D1%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A7%0AMac-n-Cheese%3A~%20nicolas%24%20let%20myvar%2B1%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A7%0AMac-n-Cheese%3A~%20nicolas%24%20let%20myvar2%3Dmyvar%2B1%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar2%0A8%0A” message=”Bash Code” highlight=”” provider=”manual”/]

Also, you can simply use the parentheses or square brackets :

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24((myvar%2B2))%0A5%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24%5Bmyvar%2B2%5D%0A5%0AMac-n-Cheese%3A~%20nicolas%24%20myvar%3D%24((myvar%2B3))%0A” message=”Bash Code” highlight=”” provider=”manual”/] [ad type=”banner”]

This allow you to use C-style programming :

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24((myvar%2B%2B))%0A3%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A4%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24((%2B%2Bmyvar))%0A5%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%24myvar%0A5%0A” message=”Bash Code” highlight=”” provider=”manual”/]

Floating point arithmetic:

You can’t do floating point arithmetic natively in bash, you will have to use a command line tool, the most common one being “bc – An arbitrary precision calculator language”.

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20bc%0Abc%201.06%0ACopyright%201991-1994%2C%201997%2C%201998%2C%202000%20Free%20Software%20Foundation%2C%20Inc.%0AThis%20is%20free%20software%20with%20ABSOLUTELY%20NO%20WARRANTY.%0AFor%20details%20type%20%60warranty’.%0A3*5.2%2B7%2F8%0A15.6%0A15.6%2B299.33*2.3%2F7.4%0A108.6%0A” message=”Bash Code” highlight=”” provider=”manual”/]

you can use the STDIN to send your formula to “bc” then get the output on STDOUT.

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20echo%20%223.4%2B7%2F8-(5.94*3.14)%22%20%7C%20bc%0A-15.25%0A” message=”Bash Code” highlight=”” provider=”manual”/]

using the here-doc notation:

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20bc%20%3C%3C%3C%20%223.4%2B7%2F8-(5.94*3.14)%22%0A-15.25%0A” message=”Bash Code” highlight=”” provider=”manual”/] [ad type=”banner”]

The “scale” variable is really important for the precision of your results, especially when using integers only (Note: you can also use “bc -l” to use mathlib and see the result at max scale) .

[pastacode lang=”bash” manual=”%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%222%2F3%22%20%7C%20bc%0A0%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D2%3B%202%2F3%22%20%7C%20bc%0A.66%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A0%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D2%3B(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A1.53%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D4%3B(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A1.5416%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22scale%3D6%3B(2%2F3)%2B(7%2F8)%22%20%7C%20bc%0A1.541666%0AMac-n-Cheese%3A~%20nicolas%24%20echo%20%22(2%2F3)%2B(7%2F8)%22%20%7C%20bc%20-l%0A1.54166666666666666666%0A” message=”Bash Code” highlight=”” provider=”manual”/]

Another way to do floating point arithmetic is to use AWK:

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20awk%20%22BEGIN%20%7Bprint%20100%2F3%7D%22%0A33.3333%0A” message=”Bash Code” highlight=”” provider=”manual”/]

You can use printf to adjust the precision of the results:

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20awk%20%22BEGIN%20%7Bprintf%20%5C%22%25.2f%5Cn%5C%22%2C%20100%2F3%7D%22%0A33.33%0A” message=”Bash Code” highlight=”” provider=”manual”/]

When using negative values, make sure to leave a white space between signs.

[pastacode lang=”bash” manual=”Mac-n-Cheese%3A~%20nicolas%24%20awk%20%22BEGIN%20%7Bprint%20-8.4%20-%20-8%7D%22%0A-0.4%0A” message=”Bash Code” highlight=”” provider=”manual”/]

Categorized in: