Site Tools


Hotfix release available: 2026-07-14a "Mort". upgrade now! [57.1] (what's this?)
New release available: 2026-07-14 "Mort". upgrade now! [57] (what's this?)
Hotfix release available: 2025-05-14b "Librarian". upgrade now! [56.2] (what's this?)
Hotfix release available: 2025-05-14a "Librarian". upgrade now! [56.1] (what's this?)
New release available: 2025-05-14 "Librarian". upgrade now! [56] (what's this?)
Hotfix release available: 2024-02-06b "Kaos". upgrade now! [55.2] (what's this?)
Hotfix release available: 2024-02-06a "Kaos". upgrade now! [55.1] (what's this?)
New release available: 2024-02-06 "Kaos". upgrade now! [55] (what's this?)
Hotfix release available: 2023-04-04b "Jack Jackrum". upgrade now! [54.2] (what's this?)
Hotfix release available: 2023-04-04a "Jack Jackrum". upgrade now! [54.1] (what's this?)
New release available: 2023-04-04 "Jack Jackrum". upgrade now! [54] (what's this?)
Hotfix release available: 2022-07-31b "Igor". upgrade now! [53.1] (what's this?)
Hotfix release available: 2022-07-31a "Igor". upgrade now! [53] (what's this?)
New release available: 2022-07-31 "Igor". upgrade now! [52.2] (what's this?)
New release candidate 2 available: rc2022-06-26 "Igor". upgrade now! [52.1] (what's this?)
New release candidate available: 2022-06-26 "Igor". upgrade now! [52] (what's this?)
Hotfix release available: 2020-07-29a "Hogfather". upgrade now! [51.4] (what's this?)
old revision restored (2026/07/04 19:45)
notes:bc_cheat_sheet

GNU bc Cheat Sheet

Overview

  • bc is a command-line calculator and primitive language
  • bc by default runs in integer mode (scale=0) and with no trig functions, running “bc -l” loads trig functions and sets scale to 20
  • bc can only take expressions on standard input (or in files), not directly on the command line e.g. echo 3+4 | bc
  • This is a great collection of bc functions. funcs.bc and logic.bc are the basic ones. This is a collection of more sanely named basic scientific calculator functions.
  • useful bash aliases
mbc ()
{
  echo $@ | bc -l -q ~/bin/extensions.bc
}
 
alias bc="bc -l -q ~/bin/extensions.bc"

So now “mbc '(3+5)/6'” works as expected

  • bc uses readline so ⇑ and ⇓ (for history) and ctrl-r (for searching) work as expected.

Basic Usage

  • Any alphanumeric variable name is allowed once it starts with a letter.
  • There are 4 special variables
    • last - The value of the last printed number
    • scale - number of decimal places to display
    • ibase - base to be used for input values
    • obase - base to be used for output values
  • Any expression that is not an assignment is displayed and stored in the last variable.
  • Every value and every expression is a number. The assignment operator returns 0. Booleans are 0 / 1.
  • Basic + - / * = == work. % is modulo . ^ is integer exponentiation.
  • Predefined functions are :
    • s - sine , c - cosine
    • a - arctan , j(n,x) - Bessel function
    • l - natural log , e - e to the power of
    • length, scale, sqrt - as named
  • && , || , ! work as expected (return 0 or 1).

Advanced Usage

  • New functions are easy to define e.g.
    define log(x,y) {
      auto retval = l(y)/l(x)
      return retval
    }

Note that the auto keyword is used to declare a local variable.

  • if(cond) else, while(cond) and for(init;cond;inc) control flow structures are all supported. break and continue are also supported.
  • read() is used to read a value from standard input. print() prints to standard output.
  • Arrays can be used by using [subscript] after the array name.
  • Special commands include quit, limits and warranty.
notes/bc_cheat_sheet.txt · Last modified: 2026/07/25 01:51 by 103.47.180.163