What does it mean to have a $"dollarsign-prefixed string" in a script?

I just saw this in an init script:

echo $"Stopping Apache"

What is that dollar-sign for?

My research so far:

I found this in the bash manual:

extquote

If set, $'string' and $"string" quoting is performed within ${parameter} expansions enclosed in double quotes. This option is enabled by default.

…but I’m not finding any difference between strings with and without the $ prefix:

$ echo "I am in $PWD"
I am in /var/shared/home/southworth/qed
$ echo $"I am in $PWD"
I am in /var/shared/home/southworth/qed
$ echo $"I am in ${PWD}"
I am in /var/shared/home/southworth/qed
$ echo "I am in ${PWD}"
I am in /var/shared/home/southworth/qed
$ echo 'I am in ${PWD}'
I am in ${PWD}
$ echo $'I am in ${PWD}'
I am in ${PWD}
$ echo $'I am in $PWD'
I am in $PWD
Asked By: Ed Brannin

||

You’re misinterpreting the manual. You’ll only see an effect when a $-quoted string is inside a ${parameter} expansion.

$ echo "${v:-'abncd'}"
'abncd'
$ echo "${v:-$'abncd'}"
ab
cd

Source and further reading: https://lists.gnu.org/archive/html/bug-bash/2005-10/msg00017.html

Answered By: Benjamin Barenblat

When a string is expanded inside of $'', the escape sequences are interpreted. From the manpage:

Words of the form $'string' are treated specially. The word expands to
string, with backslash-escaped characters replaced as specified by  the
ANSI  C  standard.

An easy example is the n escape sequence for a newline:

$ echo 'foon'
foon
$ echo $'foon'
foo

$ 

Note: You may get different results in other shells as echo may interpret escape sequences without providing options.

Answered By: jordanm

There are two different things going on here, both documented in the bash manual

$’

Dollar-sign single quote is a special form of quoting:

ANSI C Quoting

Words of the form $’string’ are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

$"

Dollar-sign double-quote is for localization:

Locale translation

A double-quoted string preceded by a dollar sign (‘$’) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

Answered By: jw013
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.