Using Google Translate From The Command Line

 1- Preliminary Note

- I'm using "Ojuba 4" Linux ("Fedora 13" based distribution).
- You don't need to be a root user to use this script.


2- What is the situation

I have found an article in one of the most famous Linux Arabic forums, it describes how to use Google Translate from the command line.


3- What is the problem

It is a good script but it is needed to write the language pairs every time and I used to make the computers work for me not to work for computers, so I didn't like to do that write every time I want to use the script, usually I translate from English to Arabic, sometimes from Arabic to English and fewer times form some other languages to Arabic or to English if I found the direct translation from that language to Arabic is not clear enough.


4- The solution

I have edited the script so now it checks the command arguments number, if it is 3 arguments it will use the first as the source language and the second as the destination language and the third is the words to translate.

But if it finds just one argument it will deal with is as the words to translate, and send to Google two times.

First to find out the source language and translate it into Arabic.

Then to find out the source language and translate it into English.

Then it ends.


5- The code itself

Here is the script after my modifications, create a new text file and call it "gtranslate" then copy and paste this code into it:


if [ $# == 3 ]
echo "From: $1 To: $2"
lynx -dump "$3&langpair=$1|$2"|awk -F'"' '{print $6}'

lynx -dump "$1&langpair=|ar"|awk -F'"' '{print "From: "$10" To: ar \n"$6}';echo

lynx -dump "$1&langpair=|en"|awk -F'"' '{print "From: "$10" To: en \n"$6}';echo


Go to the terminal -command line window- and move to the same location that you saved the script in, then make it executable by doing this:

chmod a+x gtranslate

Then move it to the folder "/usr/bin/" if you have root access, or you can move it to your local bin folder "~/bin/" if your system supports it, otherwise you can move it to your home folder and call it from there directly like this - replace "rashad" with your Linux login name:


As you can see, it uses the "lynx" to dump the web page so you will need to install it if you don't have it installed but in that case you will need root user access, like this:

su -c "yum install lynx"


6- A test drive

And here are some examples:

The long/old/original way of use:

gtranslate ar fr "????? ?????? ??????"

From: ar To: fr
Société arabe pour Linux

The short/new/my way of use:

gtranslate "Société arabe pour Linux"

From: fr To: ar
??????? ??????? ?????

From: fr To: en
Arab Society for Linux

It works :), but if you can read Arabic you will find that the phrase that I start with "????? ?????? ??????" is not the same phrase that I got at the end: "??????? ??????? ?????" even that I just translate the same result he gave to me, that is one of the reasons that I don't trust the computer translation for all the time.


7- Thank you

Thank you for reading this and thanks for HowToForge to publish.

Share this page:

Suggested articles

9 Comment(s)

Add comment


From: Anonymous

no need to give such amount permission to the script; "chmod u+x gtranslate" is sufficient in order a personnal use of the script ;)

From: Anonymous

  1. # access from terminal
  2. # mirror
  3. # mirror
  4. # requiments: curl, Mozilla js shell, account
  5. # + output: translated text, fixed text with highlight, dictionary, transcription, audio pronunciation
  6. # + cache for all words
  7. # + saving words to file for learning

From: Monsoft

Or you can use tiny script which I wrote in 10 minutes:

# Command line translator based on google translate engine
# by Monsoft
# (c) 2012 Ascot

# Check if curl is installed
curl -V >/dev/null 2>&1|| { echo "I require curl but it's not installed. Aborting." >&2; exit 1;} 

# Check internet connectivity
curl -v 2>&1|grep -m1 "HTTP/1.1" >/dev/null 2>&1|| { echo "I require internet connection. Aborting." >&2; exit 1;}
# Main

if [ $# -ne 3 ]
		echo -e "Command line language translator by Monsoft"
		echo -e "\nAvailable languages:"
		echo -e "---------------------\n"
		curl -s --user-agent "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) QupZilla/1.3.1 Safari/534.34" ""|grep -Eo 'sl=[a-z]{2}">[^>]*<'|sed 's/sl=//g;s/">/ -> /g;s/ ${TO}"
curl -s --user-agent "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.34 (KHTML, like Gecko) QupZilla/1.3.1 Safari/534.34" "${FROM}&tl=${TO}&ie=UTF-8&prev=_m&q=${SENTENCE}"|sed -n 's/.*class="t0">//;s/<.*$//p'
echo -e "\n"

From: Anonymous

Excellent script ! Please modify the script for sending a text file and writing the translated result back in a file....

From: Monsoft

Added some time ago


Very useful script. Thanks, Monsoft!

From: Anonymous

I had some similar improvements in mind when I came across a one-liner over @ I can't claim any credit for what's in google_translate function. 

Main improvement was the simple while loop. My primary use is when I'm supporting systems in a foreign language where getting the gist is more important than the nuance.

function long_lang {
case $1 in
en) echo english;;
de) echo german;;
fr) echo french;;
es) echo spanish;;
function google_translate { 
wget -qO- "$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; 

echo Translating `long_lang $1` to `long_lang $2`
echo $divider
while read; do
google_translate "$REPLY" $SRC_LANG $TGT_LANG
echo $divider

From: Jogai

The Google Translate API has been officially deprecated as of May 26, 2011. the API will be shut off completely on December 1, 2011.

From: Anonymous

Now that Google is shutting down its API, it'd be a good idea to try the Free and Open Source Apertium.

Find released language pairs:


apt-cache search apertium

 Arch Linux:

yaourt -Ss apertium


Install English<->Spanish:


sudo apt-get install apertium-en-es

Arch Linux:

yaourt -S apertium-en-es


echo "Use the Force, Luke" | apertium en-es


apertium es-en spanish_document.odt english_translation.odt