Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

How can I add a command line calculator to my bash? I have found some, but all of them use the full stop as decimal mark, but I want to have it to use the comma as decimal mark as most of the world does, see picture:

map of the world showing the use of decimal marks

(source wikipedia)

  • blue: Full stop/Period (.)
  • green: Comma (,)
  • red: Momayyez (٫)
  • gray: Data unavailable

The ones that I have found (all with full stop as decimal mark) are the following, where these lines have to be put into your ~/.bashrc file:

  1. Using bc, which has the advantage, that you can calculate ridiculously large numbers:

    calc () {
      bc -l <<< "$@"
  2. With awk, where you have mnemonic names for trigonometric and other functions and you can use fractional exponents and you can give the exponent by the two chars ** instead of the, on some keyboards difficult to type ^:

    calc () {
      awk "BEGIN { print $* ; }"
share|improve this question
I doubt it's most if it includes none of India, China or the USA (those 3 account for 40% of the world population) – Stéphane Chazelas Jan 23 '14 at 11:58
Well, if you say China, India and USA (and I say Egypt) have 40% of the world’s population, and knowing that Australia and the few remaining blue countries in Africa are close to empty, the rest (60%) uses the comma as decimal mark. q.e.d. – erik Jan 23 '14 at 15:02
Pakistan, Philippines, Nigeria, Japan, Mexico are another 10%. – Stéphane Chazelas Jan 23 '14 at 16:01
Note that , also doubles as an operator in many arithmetic expressions (C, shell, perl...). ksh93, which honours the locale's decimal_point has that issue. LC_ALL=fr_FR ksh93 -c 'echo $((1,2))' outputs 1,2 and 2 in an English locale. – Stéphane Chazelas Jan 23 '14 at 16:12

2 Answers 2

up vote 4 down vote accepted

I have found a solution.

calc () {
  awk '
    function asin(x) { return atan2(x, sqrt(1-x*x)) }
    function acos(x) { return atan2(sqrt(1-x*x), x) }
    function atan(x) { return atan2(x,1) }
    function tan(x) { return sin(x)/cos(x) }
    BEGIN { pi=atan(1)*4; print '"$(echo "$@" | tr , .)}" | tr . ,

This one

  • accepts numbers as 5,2 or 5.2 (i.e. both full stop and comma as decimal mark)
  • it uses comma as decimal mark for the output/solution
  • spaces and tabs are removed from input, i.e. you can enter easy readable calculations as input
  • the number pi is defined via 4*atan(1)
  • some common trigonometric functions are defined
share|improve this answer

There is no reliable way to implement that. The issue is the comma is used to separate arguments when more that one is passed to a function.

If you aren't going to use such functions, I guess the simplest way would be something like:

calc () {
  echo "scale=6;" "$@" | tr , . | bc -l | tr . ,

That gives:

$ calc 1 + 1
$ calc 1 / 3
$ calc "s(0,5)^2"   # sin(0.5)^2
$ calc "4*a(1)"     # PI

You can of course increase the decimal precision which is here 6 digits.

share|improve this answer
Nice. Easier to understand than the nested awk statement. But I miss calculations like 27^(1/3) and the (for me) easier to type 27**(1/3) which awk supports. – erik Jan 23 '14 at 14:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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