Explains: echo Command (echo $”string”) Double-quoted String Preceded By a Dollar Sign

by on October 22, 2010 · 2 comments· LAST UPDATED December 9, 2010

in

I noticed that many shell scripts in /etc/init.d/ directory use the following syntax:

echo $"Usage $prog start|stop|reload|restart"

Why a double-quoted string preceded by a dollar sign ($"string") using the echo command under Linux / UNIX bash scripts?

Short answer - this is done to translate given string according to the current locale.

More About Locale

Your Linux script (or UNIX commands) may be used by all over the world in various languages other than English. Linux offers support different languages. You can setup local by editing your ~/.bashrc or ~/.bash_profile file. You need to set environment variables. You can set these variables to be system-wide, or on a per-session basis as follows:

  1. LC_ALL : Overrides all LC_* environment variables with the given value.
  2. LC_CTYPE : Character classification and case conversion.
  3. LC_COLLATE : Collation (sort) order.
  4. LC_TIME : Date and time formats.
  5. LANG :

See locale(7) man page for more information.

Say Hello To Unicode

The Unicode Consortium enables people around the world to use computers in any language. The specifications set by Unicode Consortium foundation for software internationalization are used in all major operating systems, search engines, applications, and the Web. Unicode solves the problems of multiple encodings by assigning unique code points to the letters and ideographs of all of the world's modern language scripts and commonly used symbols. UTF-8 is a serialization method for Unicode that is the de facto standard for encoding Unicode on UNIX-based operating systems, notably Linux. See unicode.org for more information.

How Do I Display Current Settings?

Type the following command:
$ locale
Sample outputs:

LANG=en_IN.utf8
LC_CTYPE="en_IN.utf8"
LC_NUMERIC="en_IN.utf8"
LC_TIME="en_IN.utf8"
LC_COLLATE="en_IN.utf8"
LC_MONETARY="en_IN.utf8"
LC_MESSAGES="en_IN.utf8"
LC_PAPER="en_IN.utf8"
LC_NAME="en_IN.utf8"
LC_ADDRESS="en_IN.utf8"
LC_TELEPHONE="en_IN.utf8"
LC_MEASUREMENT="en_IN.utf8"
LC_IDENTIFICATION="en_IN.utf8"
LC_ALL=

Task: Find Out All Available Local Settings

To determine what other locale settings are available to you, type the following command:
$ locale -a
Sample outputs:

C
en_AG
en_AU.utf8
en_BW.utf8
en_CA.utf8
en_DK.utf8
en_GB.utf8
en_HK.utf8
en_IE.utf8
en_IN
en_NG
en_NZ.utf8
en_PH.utf8
en_SG.utf8
en_US.utf8
en_ZA.utf8
en_ZW.utf8
hi_IN
mr_IN
POSIX

I've installed and using the following:

  1. hi_IN - Hindi is the official languages of the Indian Union Government and of many states in India.
  2. mr_IN - Marathi language spoken by the Marathi people of western and central India.
  3. en_IN - English (default) spoken and used by the Government and Business in India.

How Do I Change My Settings?

In this example, set it to a en_US.UTF-8 (change it from Indian English to US English i.e. en_US sets the language, and utf-8 sets the code page) environment set the following values in the profile:

 
LANG=en_US.utf-8
LC_ALL=en_US.utf-8
 

Try to display the system date and time, enter:
$ date
Sample outputs:

Thu Oct 21 23:39:43 IST 2010

Now, run an application in UTF-8 Hindi locale then with bash shell, enter:
$ LANG=hi_IN.UTF-8
Display the system date and time, enter:
$ date
Sample outputs:

गुरु  अक्टूबर 21 23:41:07 IST 2010

In this example, mr_IN sets the language, and utf-8 sets the code page:
$ LANG=mr_IN.UTF-8
$ gedit

Sample outputs:

Fig.01: gedit using local Marathi language

Fig.01: gedit using local Marathi language

Shell Scripting Example

Create a shell script as follows :

#!/bin/bash
### The location of the message catalog files ###
TEXTDOMAINDIR=/usr/local/share/locale
 
### our .mo file name ###
TEXTDOMAIN=greet
 
### Translate Hello!  string according to the current locale ###
echo $"Hello!"

(Code listing - 01: greet)

Note echo command with $"..." syntax. Save and close the file. Type the following commands to create language specific files:
$ bash --dump-po-strings greet > greet.pot
Create hi (Hindi), fr (French), and es (Spanish) language specific dirs:
$ mkdir {hi,fr,es}
Copy master greet.pot, enter:
$ cp greet.pot hi/
$ cp greet.pot fr/
$ cp greet.pot es/

Update Hindi language specific file as follows:
$ cat hi/greet.pot
Sample outputs:

#: greet:2
msgid "Hello!"
msgstr "नमस्ते!"

Update French language specific file as follows:
$ cat fr/greet.pot
Sample outputs:

#: greet:2
msgid "Hello!"
msgstr "Bonjour!"

Update Spanish language specific file as follows:
cat es/greet.pot
Sample outputs:

#: greet:2
msgid "Hello!"
msgstr "Hola!"

Compile message catalog to binary format, enter:
$ sudo msgfmt -o /usr/share/locale/hi/LC_MESSAGES/greet.mo hi/greet.pot
$ sudo msgfmt -o /usr/share/locale/fr/LC_MESSAGES/greet.mo fr/greet.pot
$ sudo msgfmt -o /usr/share/locale/es/LC_MESSAGES/greet.mo es/greet.pot

Test it as follows, set execute permission on your greet:
$ chmod +x greet
Test Hindi locale version
$ LANG=hi_IN ./greet
OR
$ LANGUAGE=hi_IN ./greet
Test French locale version, enter:
$ LANGUAGE=fr_FR ./greet
Test Spanish locale version, enter:
$ LANGUAGE=es_ES ./greet
Sample outputs:

Fig.02: greet - a Bash shell script with localization support

Fig.02: greet - a Bash shell script with localization support

References:

See the following man / info pages:
$ info gettext
$ man bash
$ man 7 locale
$ man 1 locale
$ man 1 msgfmt

Also see:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 2 comments… read them below or add one }

1 Stefan Lasiewski October 22, 2010 at 5:33 pm

Why do I get question marks, and how can I resolve this? This is MacOSX 10.5 running Terminal.

$ LANG=hi_IN.ISCII-DEV
$ date
?? ?????? 22 10:32:49 PDT 2010

Reply

2 Pankaj Patel November 14, 2010 at 4:05 pm

@Stefan
This can be case that either that hindi character set is not present in the MacOSX.
Try to see the help of MacOSX that it is having hindi char set or not.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: