Thursday, May 16, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 4295  / 2 Years ago, mon, december 13, 2021, 7:24:49

Is there simple a rule to understand when to use quotes in Ubuntu command line and when single quotes and when double quotes are needed?



For example, how to explain the difference here:



$ echo $'a
aa
ac
b
cc dd
ese'
a
aa
ac
b
cc dd
ese
$


and



$ echo $"a
aa
ac
b
cc dd
ese"
a
aa
ac
b
cc dd
ese
$

More From » command-line

 Answers
4

In your case, there is a special quoting syntax used, namely $'...' and $"...". It looks like this syntax came from the Korn shell to Zsh and Bash, and is now in POSIX (see for example the "expand sequences" line in http://mywiki.wooledge.org/Bashism).
This syntax is not mentioned in the answers to Differences between doublequotes " ", singlequotes ' ' and backticks ´ ´ on commandline?.



Details on this syntax can be found in the bash(1) 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. Backslash escape sequences, if present, are
decoded as follows:
a alert (bell)
 backspace
e
E an escape character
form feed

new line

carriage return
horizontal tab
vertical tab
backslash
' single quote
" double quote

nn the eight-bit character whose value is the octal value
nnn (one to three digits)
xHH the eight-bit character whose value is the hexadecimal
value HH (one or two hex digits)
uHHHH the Unicode (ISO/IEC 10646) character whose value is
the hexadecimal value HHHH (one to four hex digits)
UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the
hexadecimal value HHHHHHHH (one to eight hex digits)
cx a control-x character

The expanded result is single-quoted, as if the dollar sign had not
been present.

A double-quoted string preceded by a dollar sign ($"string") 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.


This also means that a variable will be expanded in $"...", but not in $'...'. Please compare:



$ echo 'I am using	$SHELL.
'
I am using $SHELL.

$ echo $'I am using $SHELL.
'
I am using $SHELL.

$ echo "I am using $SHELL.
"
I am using /bin/bash.

$ echo $"I am using $SHELL.
"
I am using /bin/bash.



In the first example, neither the $SHELL variable nor the backslash escapes are expanded. In the second example, the backslash examples are expanded, but not the variable. The third and fourth examples give identical results: Only the variable is expanded.



The form $'...' can be useful to assign contents with meta-characters like or
to variables. I could not find an application for the form $"...", though.


[#26580] Tuesday, December 14, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ilityushing

Total Points: 18
Total Questions: 100
Total Answers: 113

Location: Senegal
Member since Wed, May 13, 2020
4 Years ago
ilityushing questions
Sat, Apr 1, 23, 03:07, 1 Year ago
Wed, Mar 2, 22, 12:25, 2 Years ago
Tue, Jun 15, 21, 14:34, 3 Years ago
Sun, Aug 15, 21, 18:44, 3 Years ago
;