How To Use Units: A Complete Guide To Unit Conversion In The Command Line
27 June 2026, 01:15
The `units` program is a powerful and often overlooked command-line utility available on Unix-like systems. It provides precise, fast, and scriptable unit conversions, handling everything from simple length conversions to complex combinations like acceleration, currency, and temperature. This guide will walk you through its core functionality, provide step-by-step instructions, and share practical tips to make the most of this tool.
Most Linux distributions and macOS come with `units` pre-installed. If not, you can install it easily:``` $ units Currency exchange rates from 2025-03-15 You have: 100 meters You want: feet328.08399 / 0.003048 ```
The output shows two numbers: the multiplication factor (100 meters = 328.08399 feet) and the reciprocal (1 foot = 0.003048 × 100 meters).
For quick, one-off conversions, pass units directly as arguments:``` $ units "100 meters" "feet"328.08399 / 0.003048 ```
You can also use the `-t` flag for a terse output, which prints only the conversion factor:
``` $ units -t "100 meters" "feet" 328.08399 ```
This is especially useful in scripts.
Basic conversions are straightforward:``` $ units "5 miles" "kilometers"8.04672 / 0.12427424 ```
`units` handles complex combinations like speed, density, or pressure:``` $ units "60 miles per hour" "meters per second"26.8224 / 0.037282271 ```
Use the word "per" to indicate division, or use the slash `/`:
``` $ units "60 miles/hour" "m/s"26.8224 / 0.037282271 ```
Square and cubic units are supported:``` $ units "100 square feet" "square meters"9.290304 / 0.1076391 ```
Use the caret `^` for exponents:
``` $ units "10^6 cubic meters" "acre-feet"810.71319 / 0.0012334818 ```
Temperature requires special handling because of offset scales:``` $ units "tempC(25)" "tempF" 77 ```
The `tempC()`, `tempF()`, `tempK()`, and `tempR()` functions convert between Celsius, Fahrenheit, Kelvin, and Rankine.
`units` can convert between currencies if you have an up-to-date exchange rate file. The default installation includes a currency file that updates automatically.``` $ units "100 USD" "EUR"92.14 / 0.010852 ```
To update currency rates manually:
``` $ units currency ```
Standard metric prefixes work automatically:``` $ units "5 kW" "hp"6.7051103 / 0.149142 ```
You can define temporary units in the interactive session using the `!define` command:``` !define my_unit = 42 meters ```
This is useful for project-specific conversions.
``` $ units -v ``` This shows the version and the path to the unit database file, which can be useful for troubleshooting. If you try to convert incompatible units, `units` will tell you:``` $ units "5 meters" "kilograms" conformability error 5 meters = 5 m kilograms = 0.001 m^3 kg / m^3 ```
This helps catch mistakes early.
Always quote expressions containing spaces or special characters:``` $ units "100 meters / second^2" "g"10.197162 / 0.0980665 ```
List all units in the database with:``` $ units -l ```
Or search for specific units:
``` $ units -l | grep "furlong" ```
You can mix and match units from different systems:``` $ units "1 light-year" "furlongs"4.702909e+13 / 2.126347e-14 ```
By default, `units` uses approximate conversions for some units. The `strict` flag forces exact conversions:``` $ units strict "1 inch" "cm"2.54 / 0.39370079 ```
For bulk conversions, use a loop:``` #!/bin/bash for value in 10 20 50 100; do result=$(units -t "$value meters" "feet") echo "$value meters = $result feet" done ```
Unit names are case-sensitive. `M` is mega, while `m` is milli. `C` is coulomb, not Celsius (use `tempC`). Both work: "meters" and "meter" are accepted. However, using the singular form is safer to avoid ambiguity. Don't forget spaces between numbers and units: `5miles` is not recognized, but `5 miles` works. If you define a unit with the same name as a built-in one, your definition takes precedence. Use `!reset` to clear custom definitions.The `units` program is a versatile, reliable tool for anyone who works with measurements, whether you're a scientist, engineer, student, or hobbyist. Its ability to handle complex conversions, currency rates, and custom definitions makes it far more powerful than a simple calculator or online converter. By mastering the interactive and command-line modes, using prefixes and compound units, and leveraging scripting capabilities, you can streamline your workflow and reduce errors in unit conversions. Next time you need to convert inches to parsecs or gallons to cubic light-years, remember that `units` has you covered.