Often one needs to convert values between different units of measurement, or perform calculations on values with various units. For example, to convert from imperial to metric, British Pounds to Australia Dollars, or pound-force seconds to newton seconds. Yes, you can simply perform a web search for the conversion and get an "instant answer" or some specific converter web app... but what it we had an easy way to do this offline from the comfort of the terminal?

This is where units shines. Units is a minimal command line application designed to work with calculations involving various units. It supports over 3000 units, and functionality from basic conversion between two units to unit-aware scientific calculations!

Unit conversions

So how do we use it? So let's find out how to convert 30 feet to metres as a simple example. This can be done interactively (ctrl+c or ctrl+d to quit):

$ units
Currency exchange rates from finance.yahoo.com on 2018-07-12
3048 units, 109 prefixes, 109 nonlinear units

You have: 30 ft
You want: m
	* 9.144
	/ 0.10936133
You have: ^C

Or non-interactively via args:

$ units '30 ft' m
	* 9.144
	/ 0.10936133

Now you may be thinking, what's with the two output values? Well, the first line denoted by the multiplication symbol (*) indicates the result of the conversion. The second line gives the inverse of the conversion (denoted by /). In the above example this is saying that 30 feet are equal to 9.144 metres, or 30 feet are equal to 1 metre divided by 0.109. This inverse is more useful for situations where you simply want the conversion factor between two units:

$ units feet metres
	* 0.3048
	/ 3.2808399

ie. foot = metre * 0.3 and foot = metre / 3.28

If units is unable to convert between the combinations of units on the left and those on the right, it will exit with a non-zero status and print conformability error.

$ units days gallons
conformability error
	86400 s
	0.0037854118 m^3

Units is very flexible when it comes to writing the units. foot, ft, feet, and foots are all valid ways of expressing a foot as a unit. However, if a conversion isn't working out as expected, it is worth checking that the units entered are being interpreted correctly. Enter the unit on its own as one argument to see how it is defined:

$ units asdf   # oops
Unknown unit 'asdf'

$ units km/h   # this isn't what we want!
        Definition: 1.5091902e+36 s / kg m

$ units km/hr  # that's better
        Definition: 0.27777778 m / s

$ units kph    # common abbreviations also defined
        Definition: km/hr = 0.27777778 m / s

Scientific calculator

Units doesn't just do conversions, but can perform entire calculations, while being aware of the units involved. This is awesome for word problem style maths questions.

If a car travels 20 kilometres in 15 minutes, how fast was it travelling in metres per second?

$ units '20km / 15min'
        Definition: 22.222222 m / s

Or running interactively:

❯ units
Currency exchange rates from finance.yahoo.com on 2018-07-12
3048 units, 109 prefixes, 109 nonlinear units

You have: 20km / 15min
You want:
        Definition: 22.222222 m / s
You have: ^C

Here we see two things:

  • By omitting the second argument, the definition of the units expression is printed, instead of a conversion factor.
  • The expression is reduced to simplest form, in this case resulting in 22.22 m/s, instead of a value in km/min or leaving the expression unreduced. It is useful to understand that everything is a unit, including numbers. This allows expressions such as 3 m 4 m 2 m to specify a box with those dimensions, or allowing m 3 and 3 m to be equivalent.

If we want the result as a conversion factor between a particular unit of measurement, we can enter that as the second argument:

$ units '20km / 15min' 'kilometre/hour'
	* 80
	/ 0.0125

Tips and tricks

The unit definitions are customizable and defined in a plain text file. A default (and rather large) file ships with units, and it will also read from your personal units file ($HOME/.units) if you want to add your own definitions. Run units -V to see what definitions files it is reading from. It's also interesting to scroll through the default well-commented definitions file to see how units are defined and the wide range of units available.

Units also has a range of command line arguments to tweak its behaviour. These can be useful for things like controlling the output format for use in scripts, or logging calculations to a file.

To find out more about this app, read its comprehensive man page: man units. There are some idiosyncrasies and niche features, such as support for nonlinear units, a "high priority" division operator, or output as a sum of units (useful for imperial measurements). Units is a powerful application and well worth having in the toolbox.