Last episode of Linux in the Shell discussed the use of the bc command to perform math on the command line. This episode continues in suit with a mathematical theme picking up from the last examples of converting between different number systems or units. While bc can help you convert between units if you know the formulas, there is another program which will do it all for you – units. Chances are units is not installed by default but a simple check in your package manager should allow you to add units to your daily tool set.
Like bc, if you execute the command units you will be placed into an interactive tool that asks you what you have:
In addition to being prompted units will also show you the currency exchanges rates from date and the number of supported units, prefixes, and non-linear units. To exit units press the – keys.
On most systems units stores the unit database in /usr/share/units/definitions.units. You can verify this by executing units with the -V, or –version, switch:
This file is plain text and can be read via a pager like less or more.
To begin converting between units simply enter the unit you have. For instance:
You will be prompted for the unit you want:
Units will then perform the conversion giving you two lines:
The first line, preceeded by the “*” will give you the conversion values you requested. The second line, preceeded by the “/” indicates the inversion of the conversion. In this case, 1 mile equals 5280 feet and the inversion, 0.00018939394 miles equals one foot.
The capacity of units is very large and you can easily determine what conversions are applicable to a given unit by entering “?” for “you want:”. This will generate a list of applicable units you can convert to.
The functionality of units is quite simple but the implementation can be tricky. For instance, converting between fahrenheit and celsius is not as straight forward as meters or feet:
you have: tempfahrenheit(32)
you want: tempcelsius
The value returned is 0 degress celsius. You could also use:
you have: 0 degC
you want: degF
But the value returned is 0 degree fahrenheit. How is this so?
you have: tempfarenheit(212)
you want: tempcelsius
This returns a value of 100 temp but:
you have: 212 degF
you want: degC
Returns a values of 117.77778. That does not seem right. The expectation that presenting the units in these ways indicates they mean the same thing. That tempC and degC are going to return the value of the temperature conferted from fahrenheit. While the first unit conversion using tempcelsius does this, degC on the other hand is showing the temperature change in celsius given the temperature change in fahrenheit. That is, a 212 degree fahrenheit change in temperature is equivalent to 117.77778 degree celsius change in temperature. Understanding this is something you have to pick up reading the man page. The units definition page will not easily convey this information but it does provide a wealth of knowlege on the units and how they were derived.
Similar to temperature conversions, converting between megabytes, gigabytes, and kilobytes is not as evident as you would expect. While most versions of units have tabbed completion, if you attempt to complete meg you will not megabyte but just meg. This is equivalent to megabits and will not give you the value expected if you attempt to convert 15gb into kilobytes. Instead you must use the proper binary prefixes:
- KiB = kilobyte
- MiB = megabyte
- GiB = gigabyte
- TiB = terabyte
- PiB = Petabyte
- EiBxxxxx = Exabyte
To convert 15 gb into kilobytes:
you have: 15 GiB
you want: KiB
Produces the value: 15728640
Again, if you get stumped in what you are looking for consult the definitions.units file. A quick search will probably turn up the answer.
Units has a few command line switches:
-q, –quiet, and –silent suppress the prompting of the interactive tool. You will not see the “you have:” and “you want:” prompts.
The –compact supress the printing of the “*” and “/” but still show both values.
-l, –one-line, will only show the first line of output and surpress the inverse of the conversion.
The preceeding three switches are combined in the -t, –terse, switch which is equivalent to –quiet, –compact, and –one-line. That is, no prompts and only the conversion line is shown.
You can execute the units command to perform conversions without having to go into interactive mode by providing the request on the command line like so:
units “15 feet” inches
Produces the output:
Currency exchange rates from 2012-10-24
You can use the -t switch to just get the conversion you are looking for:
The -v, or –verbose, switch will provide a bit more output than normal:
units -tv “15 feet” inches
Produces the output:
Currency echange rates from 2012-10-24
15 feet = 180 inches
This entry just touched the surface of the units command. Units is an incredibly powerful tool at your disposal. To learn more about the command consult the man and info pages and consider giving the definitions list a perusal.
- man units
- info units
If the video is not clear enough view it off the YouTube website and select size 2 or full screen. Or download the video in Ogg Theora format:
- Episode 026 – units Ogg Theora Video – Archive.org
Thank you very much!
- Ogg file hosted by Hacker Public Radio
- Speex file hosted by Hacker Public Radio
- Mp3 file hosted by Hacker Public Radio