Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An alias to ease unit conversions UNIX
An earlier hint and comments discussed using "units", a built-in UNIX tool for unit conversion. Here is a way to use the utility in a convenient way. Put this line in your aliases.mine file:
alias uni 'echo '\''scale=4; \!:1'\''`units \!:2 \!:3` |
awk '\''{print $1, $2, $3}'\'' | bc -l'
NOTE: Shown on two lines; put a space between the "|" and "awk" and remove the line break!

Now, you can get unit conversion by typing uni [value] [starting unit] [out unit]. For example:
% uni 200 nmile mile
This converted 200 nautical miles to statute miles. To find out which units are available, examine the file units.lib in /usr/share/misc/.
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)

An alias to ease unit conversions | 5 comments | Create New Account
Click here to return to the 'An alias to ease unit conversions' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
I get a "0: event not found" error with this
Authored by: rgoer on Feb 17, '02 11:22:16AM
I added this alias verbatim (replacing the newline character with just a space), but I am confronted with a "0: event not found" error when the tcsh process starts. After this error, if I try uni 5280 foot mile, tcsh asks me if I meant uniq 5280 foot mile... apparently this error screws up the alias completely.

[ Reply to This | # ]
Authored by: robg on Feb 17, '02 12:31:46PM

I used the wrong HTML entity - the story was originally posted with ] characters instead of backslashes! It's fixed now; sorry for the problem...


[ Reply to This | # ]
Authored by: mcglk on Feb 17, '02 04:13:20PM

Aliases can be useful, but for those of us that use bash, for example, tcsh aliases are problematic. Far better (and more comprehensible to boot) to have this as a simple shell script. Put it in /usr/local/bin, and you can share it with everyone on your system without having to duplicate aliases.

#!/bin/sh CONV=`units $2 $3 | awk '{print $1, $2, $3}' | head -1` echo "scale=4; $1 $CONV" | bc -l

[ Reply to This | # ]
Authored by: RandyMan on Feb 18, '02 01:24:08PM

% uni 5280 feet mile = 1.00000000320
% uni 1 mile feet = 5280


% uni 1 pound gram = 453.59237
% uni 453.59237 gram pound = .9999999900

[ Reply to This | # ]
or just use units with longer command line
Authored by: mcquaid on Feb 13, '03 07:26:29PM
you can just say
units 200-nmile mile
on the command line to get the same result as in the script. Or say
units 1-mile feet
to get 5280. The man page for units is perhaps too opaque for it to be obvious that you can do this. Notice the dash between the quantity and the unit.

[ Reply to This | # ]