Performing Unit Conversions with the units Command

If you've ever had trouble remembering the formulas to convert miles to meters or cups to gallons, you'll really like Adrian Mariano's units command, which you can find under the /usr/bin directory. Want to know how many furlongs per mile? How about how many acres are in a square mile?

One way to use the units command is from the command line, for example:

# units floz gallon

* 0.0078125 / 128

This simple example shows how to find out how many fluid ounces are in a gallon. You see that there are 128 and that a fluid ounce is less than one hundredth of a gallon. Although using the command line is handy for quick conversions, you can also run a series of queries, as follows:

# units

501 units, 41 prefixes

You have: mile*mile You want: acre

* 640

/ 0.0015625 You have: mile2 You want: acre

* 640You You

/ 0.0015625 have: mile*2 want: acre

* 640

/ 0.0015625

You You have: mile want: furlong

* 8

/ 0.125

You You have: 100 fathoms want: feet

* 600

/ 0.0016666667

In the interactive mode, you can ask units for any number of conversions. The units command works by reading its database of conversions from the file /usr/share/units.dat. You can also copy this file to your home directory and add your own custom conversions to it. As you can see, you can use different notations to indicate amounts to be converted. Another interesting feature is that units can also perform cur­rency conversions, for example:

# units dollar yen

* 107.52688 / 0.0093

Note that this might not be entirely true, as currency values change daily. You can edit the units.dat file and insert not only current currency values, but also prices for gold, silver, platinum, or pork bellies. See the units manual page for more information.

The bc command is an interpreter for a calculator language. You can use this command, by Philip Nelson, to write calculator programs while bc is running or have bc run the program after it starts. The bc language has nearly 40 operators, functions, and program­ming logic keywords. Although this section doesn't go into how to program in bc,if you're interested in the bc language, try the simple checkbook balancing program from bc's manual page. The program can work like the previous example for the dc command. Read the bc manual page, and then type the program into a file using your favorite text editor. You can run it with the following:

# bc nameofyourfile

interpreterThe bc command starts by reading in the program in the file nameofyourfile and presents the following:

Initial balance? 2500.00

current balance = 2500.00 transaction? 49.95 current balance = 2450.05 transaction? 32.18 current balance = 2417.87 transaction?