Terminal 101: Solving Math Equations
Posted 06/04/2012 at 12:30pm
| by Cory Bohon
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
This week’s Terminal 101 tip will help you solve both simple and complex equations through the command line without having to launch a dedicated application. With the Terminal and bc (a command line-based basic calculator), you’ll be able to perform quick calculations in long string formats, and even set variables and refer to them throughout the equation.
Opening bc

To open the bc environment, you’ll need to open the Terminal and type in bc followed by the enter key. Once inside the environment, you’ll be able to type mathematical statements followed by the enter key to instantly see the answer. To exit bc, type quit, followed by the Enter key.
Basic Calculations
Bc uses the same standard computer arithmetic conventions also used in programming languages to perform calculations. See the list below:
+ Addition
- Subtraction
* Multiplication
/ Division
^ Exponent
Just like in algebra, you can use parentheses "()" to separate the mathematical statement for order of evaluation. Otherwise, the statement will be evaluated with the typical mathematical precedence (multiplication and division first, followed by addition and subtraction).

So, if you wanted to multiply 12 by 6, and divide the total by 12 minus 4, then you’d type the following statement into bc:
((12 * 6) / 12) - 4
Setting and Using Variables
As you can already see, bc is a powerful calculator that allows you to string multiple mathematical statements together and instantly see the results, but sometimes you may want to set a variable. A variable is a placeholder for a number that is used frequently in your calculations.

Setting a variable is as easy as typing a name, followed by an equal sign (=), followed by the value to be stored in the variable, like this:
m = 600
a = 20
You can then perform a calculation and store it’s value in a variable, like this:
f = m * a
You can print out the contents of a variable to the screen by simply typing the variable name into bc and pressing enter.
That’s not all bc can do! To find out all how to use all that bc has to offer, including loops, type "man bc" in the Terminal.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.