LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Odd bash construction... (http://www.linuxquestions.org/questions/linux-general-1/odd-bash-construction-891593/)

dafydd2277 07-13-2011 06:30 PM

Odd bash construction...
 
Context: I'm trying to spruce up the jboss_init_redhat.sh 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:

Code:

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?!

Thanks!
dafydd

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:
Code:

#!/bin/bash

TEXTDOMAINDIR=/usr/local/share/locale
TEXTDOMAIN=hi

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

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

$ LANG=en_US.utf8
$ ./hi_locales.sh
Hello!
How are you?

And in the France French locale, I get another:

Code:

$ LANG=fr_FR.utf8
$ ./hi_locales.sh
Bonjour!
Comment allez-vous?

For more details on how to do that, check out this reference: http://www.cyberciti.biz/faq/bash-lo...-echo-command/

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.

Code:

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



All times are GMT -5. The time now is 08:40 AM.