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:
- LC_ALL : Overrides all LC_* environment variables with the given value.
- LC_CTYPE : Character classification and case conversion.
- LC_COLLATE : Collation (sort) order.
- LC_TIME : Date and time formats.
- 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:
- hi_IN - Hindi is the official languages of the Indian Union Government and of many states in India.
- mr_IN - Marathi language spoken by the Marathi people of western and central India.
- 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:
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:
References:
See the following man / info pages:
$ info gettext
$ man bash
$ man 7 locale
$ man 1 locale
$ man 1 msgfmt
Also see:
- The unicode consortium.
- A quick primer on Unicode and software internationalization under Linux and UNIX.
- Startup scripts
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop
















{ 2 comments… read them below or add one }
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
@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.