How To Use Units: A Practical Guide To Gnu Units For Conversions And Calculations
15 August 2026, 00:38
The `units` program, often referred to as GNU Units, is a powerful command-line utility for converting quantities between different units of measurement and performing arithmetic with dimensional quantities. Unlike simple online converters, `units` understands hundreds of units across categories like length, mass, time, temperature, currency, and even esoteric fields like radiation or data storage. This guide walks you through everyday usage, advanced techniques, and pitfalls to avoid.
At its simplest, `units` works as an interactive calculator. Open a terminal and type `units`. You will see a prompt like:
``` You have: ```
Type the quantity and unit you want to convert from, then press Enter. The program will ask:
``` You want: ```
Type the target unit, and press Enter. For example:
``` You have: 5 feet You want: meters1.524 / 0.65616798 ```
The first number (1.524) is the conversion factor—5 feet equals 1.524 meters. The second number (0.65616798) is the reciprocal, meaning 1 meter equals 0.65616798 feet.
For one-off conversions, you can skip the interactive mode by passing both arguments directly:
``` $ units 5 feet meters ```
This prints the same result. Note that `units` is case-sensitive: `M` is mega (10^6), while `m` is meter. Always use lowercase for meters, seconds, and grams unless you mean prefixes.
The real power of `units` lies in its ability to handle compound expressions. You can multiply, divide, and exponentiate units:
Example:
``` $ units "60 miles / hour" "meters / second"26.8224 / 0.037282272 ```
Always quote expressions containing spaces or special characters like ``, `/`, `^`, or parentheses to prevent the shell from interpreting them.
Temperature requires care because Fahrenheit and Celsius use offset scales, not just scaling factors. `units` handles this if you use the built-in functions `tempF(x)`, `tempC(x)`, and `tempK(x)`.
``` $ units "tempF(98.6)" tempC37 ```
To convert from Celsius to Fahrenheit:
``` $ units "tempC(37)" tempF98.6 ```
Important: Do not write `98.6 degF` or `37 degC` for absolute temperatures—that treats them as relative differences (e.g., a 5-degree difference). For temperaturechanges, use `degF` or `degC` directly:
``` $ units "5 degC" degF9 ```
This correctly gives 9 degrees Fahrenheit of change.
GNU Units can fetch live exchange rates if you have internet access and a `units.dat` file with currency definitions. Use the `-t` (terse) flag to output only the number, which is ideal for scripts:
``` $ units -t "100 USD" EUR ```
If you get an error about unknown currency, you may need to run `units_cur` to update the currency file. Alternatively, you can manually set rates using the `-f` flag with a custom file, but for most users, updating via `units_cur` is simplest.
1. Use `-v` for verbose output: This shows the full conversion path, including intermediate units. Useful for debugging why a conversion failed.
2. Check unit definitions: Type `units -l` to list all known units, or `units -U` to list only the units that are actually convertible to each other. To see the definition of a specific unit, use `units verbose` with the unit name:
``` $ units verbose "acre" ```
3. Use `exponent` for powers: If you need cubic meters, you can write `m^3` or `m3`. Both work.
4. Define your own units: Create a file `~/.units` (or `~/.units.dat`) and add custom units. For example, to define a "smoot" (5 feet 7 inches), add:
``` smoot 5 ft 7 in ```
Then reload `units` and use `smoot` like any other unit.
5. Use `one-line` for compact output: The `-1` flag prints the conversion factor only, useful for piping into other commands.
Because `units` reads from standard input, you can pipe conversions into it:
``` echo "100 km" | units -t miles ```
For batch conversions, write a loop in bash:
``` for value in 1 2 5 10; do echo "$value kg" | units -t lbs done ```
To incorporate `units` into Python or other programs, call it via `subprocess` and parse the output. Remember to use the `-t` flag to get a single numeric value.
GNU Units is a hidden gem of the command line. Once you master its syntax, you’ll never open a web converter again. Start with simple conversions, then gradually explore compound units, custom definitions, and scripting. The investment pays off every time you need a quick, reliable, offline conversion.