Command Line Currency Converter For Linux

currency-convertMany times I prefer to just work from the shell. The ~/.bashrc file is a great place to put functions if you are using them regularly.

Let’s build a cool command line currency converter using google’s currency converter. You might require wget or curl and sed to do this.

Just run the following command in terminal.

wget -qO- "http://www.google.com/finance/converter?a=1&from=usd&to=inr&hl=es" |  sed '/res/!d;s/<[^>]*>//g';

This will convert 1 USD to INR (Indian Rupee).
Output:

1 USD = 45.4950 INR

What we are doing is, query google using wget and parse the output using sed.

You can use curl as well:

curl -s "http://www.google.com/finance/converter?a=1&from=usd&to=inr&hl=es" |  sed '/res/!d;s/<[^>]*>//g';

Isn’t that a long command. Lets create a cool bash function for the ease use:
Open you ~/.bashrc file in your favorite text editor and add the following lines and save.

currency_convert() {
  wget -qO- "http://www.google.com/finance/converter?a=$1&from=$2&to=$3&hl=es" |  sed '/res/!d;s/<[^>]*>//g';
}

Now Run:

$ currency_convert 10 usd inr
10 USD = 454.9500 INR

If you want to convert to more than one currencies at once, rewrite our function as bellow.

currency_convert() {
  for i in $3
  do
    wget -qO- "http://www.google.com/finance/converter?a=$1&from=$2&to=$i&hl=es" |  sed '/res/!d;s/<[^>]*>//g';
  done
}

Now run:

$ currency_convert 1 usd "inr gbp eur"
1 USD = 45.4950 INR
1 USD = 0.6603 GBP
1 USD = 0.7382 EUR

Happy currency converting 🙂

8 Comments

handband2 March 20, 2010

How about a script like the following:

#!/bin/bash
## http://www.google.com/finance/converter

AMOUNT=`zenity --width=250 --title='Currency Converter' --text='Please enter US dollar (USD) amount' --entry`

if [ "${AMOUNT}" == "" ]; then
zenity --error --text="Amount not defined by user.
Exiting. "
exit 1
fi

(echo "European Euro (EUR)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=eur" | sed '/res/!d;s/]*>//g';
echo "Brazilian Real (BRL)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=brl" | sed '/res/!d;s/]*>//g';
echo "Mexican Peso (MXN)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=mxn" | sed '/res/!d;s/]*>//g';
echo "Argentine Peso (ARS)"; wget -qO- "http://www.google.com/finance/converter?a="$AMOUNT"&from=usd&to=ars" | sed '/res/!d;s/]*>//g';) | zenity --width=300 --height=300 --text-info --title "Converted Currency"

matelot March 20, 2010

I’d rather just go to the web page