Unix & Linux: What does it mean to have a $"dollarsign-prefixed string" in a script?
Channel: Roel Van de Paar
Date: 2020-08-16

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


The Question: 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

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 25 people ==
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.

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user jw013 (https://unix.stackexchange.com/users/9537), user jordanm (https://unix.stackexchange.com/users/13796), user Gilles 'SO- stop being evil' (https://unix.stackexchange.com/users/885), user Ed Brannin (https://unix.stackexchange.com/users/5639), and the Stack Exchange Network (http://unix.stackexchange.com/questions/48106). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.