How to Convert Measurements in the Terminal Using the Linux units Command

When working on engineering scripts, processing scientific data, or simply trying to figure out how many miles are in a 5-kilometer race, you often need to convert measurements between the metric and imperial systems. Instead of opening a web browser and relying on Google, you can perform highly complex, multi-variable conversions directly inside the Linux terminal using the incredibly powerful units command.

How the units Command Works

The units utility is an interactive program capable of converting thousands of different units across length, volume, mass, temperature, and even obscure physics constants. It is often installed by default on Ubuntu, but if your terminal says the command is not found, you can install it using sudo apt install units.

To launch the interactive converter, simply type:

units

The program will load its database and prompt you with: You have:. You must type the quantity and the starting measurement. Then press Enter. It will prompt you with: You want:. You type the target measurement.

For example, to convert 55 miles into kilometers:

You have: 55 miles
You want: km

The terminal will instantly output the result in two formats: the direct multiplication, and the inverse division.

        * 88.51392
        / 0.011297658

This tells you that 55 miles is exactly 88.51 kilometers.

Executing Conversions on a Single Line

While the interactive mode is great for human use, it cannot be used inside an automated bash script. To bypass the interactive prompts, you can simply provide the “have” and “want” arguments directly on the command line in a single shot.

units "100 meters" "feet"

Important Note: Because spaces separate the number from the unit (e.g., “100 meters”), you must wrap the arguments in quotation marks so the bash shell treats them as a single string. The output will look identical to the interactive mode.

        * 328.08399
        / 0.003048

Handling Temperature Conversions

Because temperatures (like Fahrenheit and Celsius) do not start at a shared absolute zero point, they require addition/subtraction to convert, not just simple multiplication. If you try to convert “100 degF” to “degC”, the units command will give you the wrong answer because it is calculating an abstract temperature interval.

To convert an absolute temperature value correctly, you must use the special tempC() and tempF() function wrappers.

You have: tempF(100)
You want: tempC

This will correctly output that 100 degrees Fahrenheit equals 37.77 degrees Celsius.

To exit the interactive program at any time and return to the standard bash prompt, type quit or press Ctrl + C.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.