132

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
0

3 Answers 3

167

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.

7
  • Thanks for the example. What I don't understand is - why did ANSI support $'' when you can just use ""? I'm sure there's a reason and for my understanding it would be good to know. Commented Jan 20, 2021 at 21:39
  • 1
    "" does not interpret that much. Try $'\x31' vs. "\x31".
    – domen
    Commented May 17, 2021 at 14:45
  • 1
    @SridharSarnobat, Another difference is: "$variable\n" will result in the contents of the variable followed by a newline, whereas $'$variable\n' will result in $variable followed by a newline. So, if you want to avoid escaping dollar signs and double quotes in a string and still be able to escape single quotes (for example if you are passing a complex command to a specific shell from within a bash script), then $'' is the way to go. Commented Mar 28, 2022 at 16:46
  • 1
    Ah yes I remember that Chinese characters' escape codes can be tab completed if you use dollar syntax. Thanks Commented Mar 28, 2022 at 17:56
  • 1
    @Palec This is for human language translation but in order for it to work you have to go through a lot of steps to set up translation files. More info under Creating Internationalized Scripts which for some reason is missing from the link above.
    – jw013
    Commented May 26, 2023 at 2:29
37

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 'foo\n'
foo\n
$ echo $'foo\n'
foo

$ 

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

1
  • 15
    There is no mention of $"...", as per the question.
    – Peter.O
    Commented Sep 14, 2012 at 7:05
-6

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

$ echo "${v:-'ab\ncd'}"
'ab\ncd'
$ echo "${v:-$'ab\ncd'}"
ab
cd

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

4
  • 6
    This doesn't answer the question: $"…" is something different. Commented Sep 13, 2012 at 23:55
  • 4
    Sorry, but your answer is wrong: there is no need to use ${} expansion to make use of ANSI quoting. Check for yourself: echo $'ab\ncd' Commented Sep 14, 2012 at 7:04
  • 1
    I think this answer was trying to explain extquote incorrectly instead of what the question was asking.
    – jw013
    Commented Sep 14, 2012 at 14:48
  • 1
    "${...}" is different than $"..." Commented Jul 27, 2017 at 22:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .