-   Linux - General (
-   -   Odd bash construction... (

dafydd2277 07-13-2011 06:30 PM

Odd bash construction...
Context: I'm trying to spruce up the file that comes with a jboss installation, so that it provides the same [ OK ] / [ FAILED ] flags that most of the other /etc/init.d/ scripts have. The file is jboss's auto start/stop for RH.

(I don't have to, but it's a good way to stretch my scripting knowledge...)

I'm using /etc/init.d/sshd as my model, and I've encountered this echo line:


echo -n $"Starting $prog: "
I get the -n. I get the $prog variable for expansion. I even get providing the space at the end of the string.

What in the world is that dollar sign supposed to be doing? Turning the whole string into a variable?!


lamouche 07-13-2011 06:42 PM

This is done for localization. It allows the string to be translated to a different language depending on your locale.

For example, after a bit of setup, I can print the output of this code in another language:



echo $"Hello!"
echo $"How are you?"

Here in the US English locale, I get one output:

$ LANG=en_US.utf8
$ ./
How are you?

And in the France French locale, I get another:


$ LANG=fr_FR.utf8
$ ./
Comment allez-vous?

For more details on how to do that, check out this reference:

dafydd2277 07-13-2011 11:09 PM

Slick! Thanks for the hint.

David the H. 07-14-2011 12:11 AM

Notice that there's also a single-quoted version, $'string', that has a different, more generally useful purpose. Any ansi-c style backslash escapes will be expanded into their literal equivalents, and the result will be a hard-quoted string. It's generally equivalent to the "-e" option in echo.


$ echo $'foo\tbar\nbaz\tbum'
foo    bar
baz    bum

All times are GMT -5. The time now is 09:14 AM.