Working with Numbers on Commandline

Basic Calculator
bc(1) is Basic Calculator, which is also a language. This is the BSD version, as there's also a GPL version.

For interactive use, run bc without arguments. Enter your basic math problems and it will output an answer. Type quit to exit. Example:
8*13*24/2
1248
quit
It can be used in place of Python, for simple math problems.

To do calculations per line, type bc -e then insert a basic math problem.

Script executables can be made with calculations to perform. Start each bc script file with:
Code:
#!/usr/bin/bc

Unit Conversion
From math/units, there's units(1).
Type units to enter interactive use:
  • enter quantity, a single space and unit
  • enter wanted unit for conversion
Press ctrl-c to exit. For units, whole words and proper abbreviations accepted.

Time/Date
sleep(1) as an egg timer.
sleep 6m; play /dir/myalarm.mp3 repeat -

cal -3 will show the previous, current and next month.

From deskutils/peaclock, peaclock will display the time in large numbers on the command line.

date(1) is one everyone's familiar with.

Health
misc/nut is an interactive nutrition tool: nut(1).


Usefulness
These can be convenient, if you want to get a quick answer or desired result by using only your keyboard (without your mouse). Also, this comes with minimal dependencies which can be common on desktop applications. Also, useful for those who don't have a graphical desktop.

Share your uses and examples of working with numbers on command line
Share how you work with numbers on the command line, including with interactive use. Python and Julia use may be included. Short scripts, and purposes for them, is ok too.
 
Despite our builtin(1) man page not covering it, we have let in sh(1)

sh:
> sh -c 'let 1+1'
2
> sh -c 'let 3*25'
75
> sh -c 'let 25%3'
1
> sh -c 'i=1024; log_i=0; while test $i -ne 0; do printf "i=%-4d log_i=%-4d\n" $i $log_i; let i=$i/2 > /dev/null; let log_i=$log_i+1 > /dev/null; done;'
i=1024 log_i=0   
i=512  log_i=1   
i=256  log_i=2   
i=128  log_i=3   
i=64   log_i=4   
i=32   log_i=5   
i=16   log_i=6   
i=8    log_i=7   
i=4    log_i=8   
i=2    log_i=9   
i=1    log_i=10

unlike its zsh and bash implementations, our sh let prints it's result to stdout even when asked to assign the result to an environment variable (e.g. let i=1+1), so it has to be piped into /dev/null to prevent it from poluting the terminal.
 
Despite our builtin(1) man page not covering it, we have let in sh(1)
That’s a bug. I wasn’t aware sh(1) supported let. Care to file a Problem Report (if there isn’t one already)?​
sh let prints it's result to stdout even when asked to assign the result to an environment variable
Or just use § Arithmetic Expansion. It’s silent by default.​
Bash:
x=$((x + 5))
so it has to be piped into /dev/null to prevent it from [polluting] the terminal.
Closing standard output (and standard error for the then emitted error message) works too.​
Bash:
let x=5 >&- 2>&-
 
math/amath, amath(1), is another BSD licensed command-line calculator. It has more functions than bc, for instance, trigonometric functions. To use it as a shell, type amath --shell.

BC needs to use an extended library to use additional functions. math/gh-bc is a BSD licensed calculator with GNU extensions. GNU's version of bc is at math/gnubc.

math/fend is another permissively licensed calculator with more advanced functions than bc. It also works with units. It lacks a manpage, other documentation files and a help command: its documentation is at https://printfn.github.io/fend/documentation/. Like bc, it can be used as a shell, or it can be used per line.

dc is a calculator, but I'm not sure it's a language like bc. I'm not sure if it uses bc underlying it.

The above play example in a previous post needs audio/sox, but you can use another commandline music player. Perhaps there's more examples of how sound can be used with numbers on the commandline.
 
It's not <<quite>> on the command line, being a curses based TUI program, but I always had a soft spot for sc, the spreadsheet calculator. Very useful when you need to spin up a spreadsheet in a terminal, or if you only have console access to a machine, I still use it quite often. I believe it was originally written by Ray Gosling, who later invented Java.


There is a currently maintained version here:- https://github.com/n-t-roff/sc

Side note: wiki says the first release of sc was in 1981, written in C, as now; sc is 44 years old this year. :)
 
dc is a calculator, but I'm not sure it's a language like bc.
Yes, same as bc(1), but you have to use the RPN notation:
Code:
dice@fbsd-test:~ % echo "12 1024 * p" | dc
12288
dice@fbsd-test:~ % echo "12*1024" | bc
12288
I'm not sure if it uses bc underlying it.

It's actually the same executable:
Code:
dice@fbsd-test:~ % ls -li /usr/bin/bc /usr/bin/dc
153722 -r-xr-xr-x  2 root wheel 219720 Jun  5 18:12 /usr/bin/bc
153722 -r-xr-xr-x  2 root wheel 219720 Jun  5 18:12 /usr/bin/dc
 
It's not <<quite>> on the command line, being a curses based TUI program, but I always had a soft spot for sc, the spreadsheet calculator.
Curses too. Better here than on a separate thread. It also makes this topic more rich, and curses is on the terminal console as well without needing a graphical desktop.

math/sc - sc(1), psc(1)
math/sc-im is a fork with more features, like additional console colors - sc-im(1)
sc-im.jpg

Uses xlsx files. No mention of OpenDocument's open standard of ODS.

math/xspread has a TUI interface, despite the name. It has an X version too. xspread(1). It comes with pxspread(1) for preparing data for xspread. math/oleo is a terminal spreadsheet which is difficult to use, and it lacks documentation.


bc executable compared to dc
It's actually the same executable
ls -l /usr/bin/[bd]c shows same file size.
diff /usr/bin[bd]c shows no difference.
dupes /usr/bin/ doesn't show matches for files.

Where's the difference between these two files that makes dc use Reverse Polish Notation. It's at the filename or shell level?
 
[…] Where's the difference between these two files that makes dc use Reverse Polish Notation. It's at the filename or shell level?
They are identical files, the ls(1) SirDice showed includes ‑inode numbers (“file serial numbers”), here 153722. The distinction seems to be made via​
C:
#define BC_IS_BC (vm->name[0] != 'd')

/// Returns true if dc is running.
#define BC_IS_DC (vm->name[0] == 'd')
in contrib/bc/include/vm.h (and the use of said defines). The vm->name gets set in main.c. The more common method, however, seems to be comparing getprogname(3), for instance in the implementation of reboot(8):​
C:
	if (strstr(getprogname(), "halt") != NULL) {
		dohalt = true;
		howto = RB_HALT;
	} else if (strcmp(getprogname(), "nextboot") == 0) {
 
In v7 unix bc was just a frontend that fork-execed dc and fed commands to it after converting from infix to postfix.

But if you want a really fancy calculator that can plot graphs & do symbolic mathematics, install math/maxima, or math/wxmaxima which even has a GUI frontend!
 
But if you want a really fancy calculator that can plot graphs & do symbolic mathematics, install math/maxima.
Is maxima a command line program? The version on FreeBSD uses a graphical toolkit as a dependency. If there's no CLI flavor, it needs its own thread of numerical programs on a GUI.

However, there's a cli program for plotting: math/gnuplot-light, gnuplot(1). The full program with a GUI is math/gnuplot.

gnuplot
Code:
gnuplot> set terminal xterm
gnuplot> plot sin(x)
gnuplot.jpg

Results, output across two monitors. While a cli version of a program, it's on a light window manager. Haven't tried this on an actual terminal console.

math/bitwise will convert a number to different bases of decimal, hexdecimal, octal, binary:
bitwise.jpg

bitwise(1).


math/basic-stats is a terminal program for statistics: basic-stats(1). This uses a BSD license. If you have a screenshot of this, please show it.

math/calc is an advanced terminal calculator with an LGPL license: calc(1), /usr/local/share/calc/README.
 
Is maxima a command line program?
Yes. But it can use gnuplot etc.
Code:
$ maxima
;;; Loading #P"/usr/local/lib/ecl-24.5.10/sb-bsd-sockets.fas"
;;; Loading #P"/usr/local/lib/ecl-24.5.10/sockets.fas"
Maxima 5.47.0 https://maxima.sourceforge.io
using Lisp ECL 24.5.10
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) factor(2^63-1);
                           2
(%o1)                     7  73 127 337 92737 649657
(%i2) factor(%o1+2);
                            3
(%o2)                      3  19 43 5419 77158673929
(%i3) factor(x^2-2*x+1);
                                          2
(%o3)                              (x - 1)
(%i4) plot3d (sin(2*theta)*cos(phi), [theta,0,%pi], [phi,0,2*%pi],[transform_xy, spherical_to_xyz], [grid, 30, 60], nolegend)$
The last will show a 3d graph in an X11 window. You can use describe(plot3d); to see what all you can do with plot3d.
 
Back
Top