Every now and then I need to do simple arithmetic in a command-line environment. E.G. given the following output:

Disk /dev/sdb: 256GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system     Name  Flags
 1      1049kB  106MB   105MB   fat32                 hidden, diag
 2      106MB   64.1GB  64.0GB  ext4
 3      64.1GB  192GB   128GB   ext4
 5      236GB   256GB   20.0GB  linux-swap(v1)

What's a simple way to calculate on the command line the size of the unallocated space between partition 3 and 5?

What I've tried already:


bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 

where the bold above is all the stuff I need to type to do a simple 236-192 as bc 1+1 echoes File 1+1 is unavailable.


expr 236 - 192

where I need to type spaces before and after the operator as expr 1+1 just echoes 1+1.

  • 6
    If you're intending to do this interactively, you don't need to type quit into bc; pressing ctrl-D will do, as it will in any interactive program.
    – marcelm
    Commented Nov 6, 2018 at 19:53
  • 5
    dc is (IMO) far superior to bc. Commented Nov 6, 2018 at 22:53
  • 2
    I think the unallocated space between ptn 3 and 5 should be 236 - 192, and not 236 - 128? Commented Nov 6, 2018 at 23:02
  • @DigitalTrauma good eye! (It was just an example and no one spotted that one!)
    – Fabby
    Commented Nov 7, 2018 at 0:10
  • 2
    @JanM. I'm the one who upvoted.... ;-)
    – Fabby
    Commented May 3, 2019 at 19:17

21 Answers 21


You can reduce the amount of verbosity involved in using bc:

$ bc <<<"236-192"
$ bc <<<"1+1"

(assuming your shell supports that).

If you’d rather have that as a function:

$ c() { printf "%s\n" "$@" | bc -l; }
$ c 1+1 22/7

(-l enables the standard math library and increases the default scale to 20.)

Store the c definition in your favourite shell startup file if you want to make it always available.

  • 6
    bc<RET>236-128<RET>quit<RET> is 16 keystrokes. bc<<<"236-128"<RET> is 15 keystrokes. Not what I would call "greatly reduced amount of typing" :-) In addition, bc can be exited with ^D, so first option is reduced to 13 keystrokes (counting ^D as two).
    – L. Levrel
    Commented Nov 6, 2018 at 16:24
  • 4
    @L.Levrel With the function it's just c 1+1 which is just what I needed! :-) (answer updated to make that clearer)
    – Fabby
    Commented Nov 6, 2018 at 16:49
  • And if you do floating point : c 'scale=2; 17/3' (or better, have scale a variable used inside the 'c()' function, and define it's value whenever you need to have n decimal digits. default is 0.) Commented Nov 7, 2018 at 14:03
  • I find the here-string approach quite useful, since you can easily recall and edit previous calculation right from your shell history. Moreover, on bash you don't even need to add the quotes.
    – gerlos
    Commented Sep 14, 2022 at 9:53


There are several solutions listed (shell, awk, dc, perl, python, etc.).

A function may be defined with any option (gawk seems to be the most flexible to use)

c () { local in="$(echo " $*" | sed -e 's/\[/(/g' -e 's/\]/)/g')";
       gawk -M -v PREC=201 -M 'BEGIN {printf("%.60g\n",'"${in-0}"')}' < /dev/null

And use it like:

$ c 236- 192


The simplest calc in CLI is the CLI (shell) itself (If IFS is default):

$ echo $(( 22 + 333 ))

Spaces could be omitted:

$ echo $((22*333))

As it is part of POSIX almost all shells have it. And it includes most of C language math functionality (except that zsh has a different precedence, set C_PRECEDENCES to restore it to a compatible value):

$ echo $((22*333^2))

And some shells have most of the C language math syntax (including comma):

$ echo $((a=22,b=333,c=a*b,c))

But it is only integer math (and usually less than 263 in present day OSes) in some shells:

$ echo $((1234/3))

$ zsh -c 'echo $((2**63))'

Some shells could do floating math:

$ ksh -c 'echo $((1234/3.0))'

$ ksh -c 'echo $((12345678901234567890123/3.0))'

Avoid zsh (zcalc has similar problems):

$ zsh -c 'echo $((12345678901234567890123 + 1))'
zsh:1: number truncated after 22 digits: 12345678901234567890123 + 1

I recommend you to avoid expr, it needs weird escapes sometimes:

$ expr 22 \* 333


At the next level is (also POSIX)bc (cousin of RPN dc)

$ echo '22*333' | bc

$ echo '22 333 * p' | dc

The dc was (historically) used to implement bc and got excluded from POSIX in 2017.

Shorter if your shell supports it:

$ bc <<<'22*333'

Or even:

$ <<<'22*333' bc

Both are arbitrary precision calculators with some internal math functions:

$ bc <<<2^200

$ echo 's(3.1415/2)' | bc -l        # sine function


After those really basic calc tools, you need to go up to other languages

$ awk "BEGIN {print (22*33)/7}"

$ perl -E "say 22*33/7"

$ python3 -c "print(22*33/7)"

$ php -r 'echo 22*33/7,"\n";'


You may define a function of any of the above options:

c () 
    local in="$(echo " $*" | sed -e 's/\[/(/g' -e 's/\]/)/g')";
    gawk -M -v PREC=201 -M 'BEGIN {printf("%.60g\n",'"${in-0}"')}' < /dev/null

And use:

$ c 22* 33 /7                   # spaces or not, it doesn't matter.
  • dc was never POSIX; this seems to be a common misconception. However, see the Rationale for bc in the 2001 edition of POSIX (pubs.opengroup.org/onlinepubs/9699919799/utilities/…) where it says that standardizing dc was considered, but not done. Also, in the link you gave, dc is listed as a excluded utility, not a removed utility. Commented Mar 6, 2020 at 16:06
  • 3
    Another useful tip if you define a function like c() above, is to add "alias calc=noglob c" - that will let you use the asterisk in expressions without quoting it, eg "calc 3*4". Works in zsh, maybe others.
    – Mike B
    Commented Sep 1, 2020 at 9:39
  • @isaac: Thank you for the complete answer. I like the function very much! I use it all the time. Could it be that there is not possible to write c 1 * 2? In my console (bash) it returns an syntax error.
    – alex
    Commented Jun 18, 2021 at 22:31
  • 2
    Yes, that is not possible as the '*' gets interpreted by the shell and is expanded as a glob to the list of all files in the present working directory. You could quote the * like this (for example): c 1 \* 2 (note the \ used). Or quote the whole string: c '1 * 2' . @alex
    – user232326
    Commented Jun 18, 2021 at 22:38

Reading this pages comments, I see a UNIX/Linux program called calc that does exactly what you want. If on Debian / Ubuntu / derivatives:

sudo apt-get install apcalc

or on Fedora Linux:

sudo dnf install calc

then you can:

calc 236-192

and if you add an alias alias c='calc' to your .bashrc or /etc/bash.bashrc then it just becomes:

c 1+1

on the command line.


In zsh:

$ autoload zcalc # best in  ~/.zshrc
$ zcalc
1> 1+1
2> ^D
$ zcalc 5+5
1> 10

The units program, whilst not intended to be used as a calculator, actually works fairly well as one.

$ units "236-192"
    Definition: 44

If there are spaces in the expression, then the expression must be quote-protected.
It supports exponentials and deep nesting of brackets

  • 4
    It's versatile: units -t -d 10 "236GB - 192GB" GB outputs 44, units -t -d 15 "236GB - 192GB" bytes outputs 44000000000, etc.
    – agc
    Commented Nov 8, 2018 at 4:58

Personally, I like libqalculate (the command-line version of Qalculate).

$ qalc
> 236-192

  236 - 192 = 44

While the interface is certainly simple, (lib)qalculate is a powerful, full-fledged calculator. e.g.

> fibonacci(133) to hex

  fibonacci(133) = approx. 0x90540BE2616C26F81F876B9

> 100!

  factorial(100) = approx. 9.3326215E157

> sin(pi)

  sin(pi * radian) = 0

It also does useful things like tab completion, open/close parentheses when necessary, and prints its interpretation of the query.

> 18-2)/4

  (18 - 2) / 4 = 4

To exit, I simply press Ctrl+d.

For even quicker access, set it to something like alias ca='qalc'.


As remarked in a comment to an earlier reply, the standard shell (ba)sh allows to evaluate arithmetic expressions within $((...)). I could not double-check whether this is part of the POSIX standard, but I did check that it also works on Cygwin and the Mingw32 shell.

To see the result, you'd indeed have to type echo $((...)), which makes some characters more than (interactive use of) bc. However, to use the result in a script, this will most probably be shorter than the bc solution (which could be, e.g., `echo ...|bc`).

Concerning verbosity, the bc command allows the option -q which suppresses output of the "normal GNU bc welcome".

As a final, slightly borderline remark, let's note that bc is not just a calculator but rather a full-fledged programming language (including user defined functions, while & for loops, etc etc). Another fact that suggests to prefer the build-in arithmetic capabilities for such simple calculations, rather than an external program. That said, extracting the data for given partition number(s) and dealing with "M", "G" suffixes, as the original question seemed to ask for, might call for (g)awk rather than bc.

Sources: https://www.gnu.org/software/bc/manual/html_mono/bc.html https://www.gnu.org/software/gawk/manual/html_node/Getting-Started.html


What I do in zsh is:

$ <<< $(( 236 - 192 ))

In bash, I'd have to explicitly mention cat:

$ cat <<< $(( 236 - 192 ))

If I wanted the result to include fractional digits (works in zsh, not in bash), I'd add a radix point to one of the operands

$ <<< $(( 236 / 128 )) 
$ <<< $(( 236. / 128 ))
  • 8
    echo $((236 - 128)) works too ;-). Commented Nov 6, 2018 at 18:03
  • Avoid zsh: zsh -c 'print $((12345678901234567890123 + 1))' zsh:1: number truncated after 22 digits: 12345678901234567890123 + 1 -1363962815083169259.
    – user232326
    Commented Nov 7, 2018 at 10:07
  • 1
    @Isaac at least zsh tells you it’s truncating; bash just gives you an incorrect answer without complaining. Commented Nov 7, 2018 at 14:21
  • @StephenKitt The problem with zsh is more complex than one output shows. Compare: zsh -c 'print $((12345678901234567890123 + 1))' and zsh -c 'print $((123456789012345678901 + 1))'. It is truncating at different lengths and producing different numeric results.
    – user232326
    Commented Nov 7, 2018 at 14:36
  • @StephenKitt Instead Bash is following the manual (and the C language about overflow of a signed int). Not that I personally like it, but it is documented as so. Try bash -c 'echo $((1<<63))' and ``bash -c 'echo $(((1<<63)-1))'`.
    – user232326
    Commented Nov 7, 2018 at 14:40

Python open in another tab?

Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on 
Type "help", "copyright", "credits" or "license" for more information.
>>> 3+3

Have you tried concalc?

Description: console calculator concalc is a calculator for the Linux console. It is just the parser-algorithm of extcalc packed into a simple console program. You can use it if you need a calculator in your shell. concalc is also able to run scripts written in a C-like programming language.

$ concalc 1+1
$ concalc sqrt2

Install with:

sudo apt-get install concalc

Before any of the other brilliant answers were posted, I ended up creating the script /usr/local/bin/c containing:

IFS=' '               # to be on the safe side, some shells fail to reset IFS.
if [ "$#" -eq 0 ];  then
    echo "$(basename "$0"): a (very) simple calculator."
    echo "type $(basename "$0") expression to evaluate (uses bc internally)"

printf '%s\n' "$*" | bc -l  # safe for most shells
                            # we may use 'bc -l <<<"$*"` for ksh, bash, zsh

so: typing c 1+1 yields 2! :-)

Note 1: I used c because that command does not exist on any Unix system that I could find. If you would have aliased that to your c compiler, use anything else that is short and you don't use.
Note 2: Source

  • 3
    (1) Decided to use echo "$*" | bc -l as the shebang you used was /bin/sh and I don't know if you have ksh/bash/zsh available. (2) Note that you may change the scale to calculate is you execute `c 'scale=60;777/333', for example. Hope it helps @Fabby
    – user232326
    Commented Nov 12, 2018 at 16:49

dc -e '236 192-p'

... of course, if you're not familiar with dc and you require more than subtracting two numbers, you'll spend more time looking up how to use dc (and maybe RPN in general) than you'll save with more familiar methods.

  • Yeah, had a 3-minute look at Desk Calculator and knew it wasn't for me: I was a TI, not an HP student.
    – Fabby
    Commented Nov 7, 2018 at 22:02

If you have gradle installed then you have groovy...

If groovy is pathed correctly you should be able to use:

groovy -e "println 1+1"

This may seem a bit redundant with all the other examples, but:

  • groovy is a powerful language
  • possibly the best library support available
  • powerful and simple math functions (Like arbitrary precision math)
  • uses redirectable stdout for it's output so it is amazingly flexible (great to use inside batch files with backticks `` and the like).

If you don't have java installed it's probably not worth installing groovy & java--it's just an option if groovy is already available.

  • Nope, not really, but a neat idea anyway! :-) +1
    – Fabby
    Commented Nov 6, 2018 at 22:11

Just because Python was mentioned a few times in other answers, here the generalized answer, summarizing the basic idea:

You can use many interpreted languages with a repl as your calculator. This saves you the learning curve for a "special" program, such as bc.

For example if you already happen to know lisp, you could use that as your quick and dirty command line calculator:

sbcl --eval '(progn (print (+ 1 2 3 4)) (quit))'

Or Ocaml, as another example:

echo "let d = 553 - 226 in print_int d" | ocaml -noprompt -stdin

Whatever language deems you most convenient.


2020 answer


# Calc
  echo "scale=2; $*" | bc -l

to your ~/.aliases, then

$ c 1/3

(The original issue has been solved multiple times already. However, the title "Simple command-line calculator" may require division to some decimal points. The use of the -l option with bc seems to give 20 decimal points, which is perhaps too many for a 'simple' calculator. This answer, which frankensteins answers from @stephenKitt and @sjas was the simplest solution I found.)


If python is installed, you can do a lot of mathematical operation through command line. I tried to provide some example below.

I have used python3 you can use python. The difference between python and python3 occur when divided(fractional) operation occur, to avoid the issue see below python vs python3.

Note: Latest all linux dist comes with both Python 2.7 and Python 3.5 by default. In case if require to install python click here.

Add, Subtract, Multiply & Divide:

$ python3 <<< "print(12+3)"
$ python3 <<< "print(12-3)"
$ python3 <<< "print(12*3)"
$ python3 <<< "print(12/3)"

Modulus -remainder of the division:

$ python3 <<< "print(14%3)"

Floor division:

$ python3 <<< "print(14//3)"

Exponent - x to the power of y (x^y):

$ python3 <<< "print(3**2)"

Square root (ex: √4 = 2):

$ python3 <<< "print(4**0.5)"

More scientific part, you will require import math library. Ex:

The natural logarithm of x = log(x):

$ python3 <<< "import math; print(math.log(4))"

The base-10 logarithm of x = log10(x):

$ python3 <<< "import math; print(math.log10(10))"

Factorial (ex: 3! = 3.2.1 = 6):

$ python3 <<< "import math; print(math.factorial(3))"

Trigonometry- sin(x), cos(x), tan(x):

$ python3 <<< "import math; print(math.sin(90))"

Fore more math related functions check here.

python Vs python3:

-For divide: (use float):

$ python <<< "print(10.0/3)"

-instead of

$ python <<< "print(10/3)"

Also you can use direct terminal:

$ python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 1+3
>>> import math
>>> math.log10(4)
>>> math.sqrt(4)

That's it. Happy coding!


There are a lot of great answers here, I actually have written a small programming language with this problem in mind, where in typing in the command line ./Ascal 2+2 or ./Ascal "log(10)" you will immediately get the result Final Answer: 4 for 2+2, and I'm sure you can imagine for log base 10 of 10. Just something to keep in mind, it's also a full programming language with a bunch of useful functions built in, plotting in the console, and taking reimann sums of functions you have defined. Check it out the project now comes with a build script so all you need to do is add it to your path variable, or add an alias in your bash config and you have an easy to use calculator!
I'd also be really appreciative of any comments anyone interested enough to check it out might have, hope it can be useful.

$ echo $((1+5))

This is Bash's Arithmetic Expansion


Either bc or qalc.

To automatically have bc always rounding up to two digits: (running it with -l is unwieldy for day to day stuff)

Edit your ~/.bashrc:

alias bc="BC_ENV_ARGS=<(echo "scale=2") \bc"

Open a new shell and run bc and be glad.


Use perl / python under the hood.

The advantage?

You get the full power of all math operations

❯ calc 'log(e)'
❯ calc 'pi*2'
❯ calc 'pi**2'
❯ calc 'pi**pi'

If you stick to string inputs, you don't have to worry about glob expansions of your shell

For zsh or bash have a function something like

function calc {
    python3 -c "from math import *; print(eval($*))"

For tcsh add alias something like

alias   Calc            'python3 -c "from math import *; print(eval(\!*))"'

Creating a one-liner:

$ c () { echo $(( ${1} )) }

Now you can use simple integer math:

$ c 1+1

$ c 25*4

$ c 25*4-10

$ c 20*5/4
  • 2
    This is already discussed in Isaac’s answer. Commented Jun 10, 2019 at 10:53
  • 2
    @StephenKitt Yes I upvoted Isaac's very detailed answer. I thought a one-liner summarizing some of what he posted would be helpful. The ${1} parameter usage is unique to all answers. Commented Jun 10, 2019 at 10:57
  • 4
    I encourage you to look into the reasons other answers don’t use ${1} ;-) Commented Jun 10, 2019 at 11:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .