58

You can greatly reduce the amount of verbosity involved in using bc: $ bc <<<"236-192" 44 $ bc <<<"1+1" 2 (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 2 3.14285714285714285714 (-l enables the standard math library and ...


44

dc is a very archaic tool and somewhat older than bc. To quote the Wikipedia page: It is one of the oldest Unix utilities, predating even the invention of the C programming language; like other utilities of that vintage, it has a powerful set of features but an extremely terse syntax. The syntax is a reverse polish notation, which basically means that ...


37

Like this: echo $(( 0xA ^ 0xF )) Or if you want the answer in hex: printf '0x%X\n' $(( 0xA ^ 0xF )) On a side note, calc(1) does support xor as a function: $ calc base(16) 0xa xor(0x22, 0x33) 0x11


35

Summary 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 ...


26

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 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.


22

With any POSIX shell: $ printf '%#x\n' "$((0x11 ^ 0x22))" 0x33


17

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


14

gdb has powerful expression calculator: gdb -q -ex 'print/x 0xA ^ 0xF' -ex q A shell function: calc_gdb() { gdb -q -ex "print/x $*" -ex q;} calc_gdb 0xA ^ 0xF $1 = 0x5


13

You can control the scale that bc outputs with the scale=<#> argument. $ echo "scale=10; 5.1234 * 5.5678" | bc 28.52606652 $ echo "scale=5; 5.1234 * 5.5678" | bc 28.52606 Using your example: $ bc <<< 'scale=2; 1.5 * 1.5' 2.25 You can also use the -l switch (thanks to @manatwork) which will initialize the scale to 20 instead of the default ...


10

A basic difference between the two is that dc uses the reverse Polish notation. It requires explicit commands even in order to produce an output. You might add two integers in bc by saying: bc <<< "2+4" and it would produce 6 on a line by itself. However, in dc you'd need to say: dc <<< "2 4 +p" You can also do much fun stuff using ...


9

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


8

For Integer arithmetic (where 3/2=1) bash echo $(( 1+1 )) fish math 1+1 zsh* echo $((1+1)) *: and ksh93, yash For floating point arithmetic (where 3/2=1.5) bash awk "BEGIN {print 10/3}" (low precision) bash echo "10/3"|bc -l (high precision) fish math -s4 10/3 zsh* echo $((10./3)) *: and ksh93, yash You can of course configure your shell to use awk ...


7

It is interpreting C and A as units. (Coulombs and Amperes.) The button where you selected “Hexadecimal” only applies when displaying the result. It doesn't affect the way the expression that you enter is parsed. Try 0xC - 0xA.


7

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(...


7

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 ...


6

As in intermixing arrays and stacks. In the example register a is used both as an array and a as a stack. 1 0:a 0 Sa 2 0:a La 0;a p First :a - register a is treated as an array. Then Sa - register a is treated as a stack. In effect pushing the array from from pt 1. down and creating a new array. As by man: Note that each stacked instance of a register ...


6

Gnuplot gnuplot - an interactive plotting program Follow the above link or type gnuplot form the prompt then help inside the gnuplot interpreter. Gnuplot is a program born to plot data, but can be used for calculation too. It offer the advantage that you can define functions and or use the built-in ones. echo "pr 20+5/2" | gnuplot # Lazy-note ...


6

What I do in zsh is: $ <<< $(( 236 - 192 )) 44 In bash, I'd have to explicitly mention cat: $ cat <<< $(( 236 - 192 )) 44 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 )) 1 $ <<< $(( 236. / 128 )) 1.84375


6

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 win32 Type "help", "copyright", "credits" or "license" for more information. >>> 3+3 6 >>>


5

The parentheses are special to the shell (they are used for creating subshells). To use parentheses in the argument to wcalc (or any other characters that are special to the shell, such as spaces, filename globbing patterns, redirection operators, pipe symbols etc.), you need to quote the arguments: $ wcalc abs(-2) sh: syntax error: `(' unexpected $ wcalc ...


5

c= echo $a | awk -F : '{print ($1 * 32140800) + ($2 * 2678400) + ($3 * 86400) + ($4 * 3600) + ($5 * 60) }' You're missing the command substitution here. This will just run the echo | awk pipeline, while setting c to the empty value in the environment of echo. You used a command substitution below, in duration=$(echo "$d - $c"), that's what you need here, ...


4

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 2 $ calc 1 / 3 ,333333 $ calc "s(0,5)...


4

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 ...


4

Qalculate Install the qalculate-gtk package: Qalculate! is small and simple to use but with much power and versatility underneath. Features include customizable functions, units, arbitrary precision, plotting, and a graphical interface that uses a one-line fault-tolerant expression entry (although it supports optional traditional buttons). apt install ...


4

Before any of the other brilliant answers were posted, I ended up creating the script /usr/local/bin/c containing: #!/bin/sh 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 ...


4

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 2 $ concalc ...


3

You have to remember that dc is a compiler - and a crazy old one at that. It's a kind of machine language - a stack-oriented calculator. It's pretty powerful, but its interface is not designed for you - it's designed to efficiently process the instructions that you write in some other language after all of the user-friendliness has been machine processed out ...


3

bash and GNU date #grab today's date in YYYYMMDD format today=$(date +%Y%m%d) #grab date as of 3 months ago in YYYYMMDD format three_months_ago=$(date +%Y%m%d --date='3 months ago') #now convert dates to "seconds since epoch" format, and then divide the difference by 60*60*24 to convert from seconds to days printf '%d\n' $(( ($(date --date=$today +%s) - \ ...


Only top voted, non community-wiki answers of a minimum length are eligible