3

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\naa\nac\nb\ncc\tdd\ne\se'
a
aa
ac
b
cc  dd
e\se
$

and

$ echo $"a\naa\nac\nb\ncc\tdd\ne\se"
a\naa\nac\nb\ncc\tdd\ne\se
$
DK Bose
  • 42,548
  • 23
  • 127
  • 221
  • I do see why this question was marked as duplicate, probably guessing the intent of the author right. But in fact it addresses a different problem. The referenced question is about "..." vs. '...' vs. \...``, this question is about $"..." vs. $'...' (note the dollar sign before the quotes). – Dubu Mar 12 '14 at 12:25
  • @Dubu, don't worry. You provided the exact answer and I'm grateful for that. – DK Bose Mar 12 '14 at 12:27

4 Answers4

2

Normally, double quotes (" ") are used for print the value of any variable and single quotes are used to print the exact string.

For example, you have a variable foo which contains pwd as value.

foo=pwd

now, when you do:-

  • echo "$foo" - it will print the value of variable foo i.e. pwd
  • echo '$foo' - it will print $foo (exact string passed in single quotes Also, Back quotes are used in command line/shell scripting, which is normally used to run the command stored in variable.
  • echo `$foo ` will print your current directory, i.e. output of pwd command.
  • 1
    Note that single quotes work like double quotes when inside double quotes: echo "'$foo'". This is useful when multiple quoting is requiried, – Adobe Mar 11 '14 at 06:17
2

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)
              \b     backspace
              \e
              \E     an escape character
              \f     form feed
              \n     new line
              \r     carriage return
              \t     horizontal tab
              \v     vertical tab
              \\     backslash
              \'     single quote
              \"     double quote
              \nnn   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\t$SHELL.\n'
I am using\t$SHELL.\n
$ echo $'I am using\t$SHELL.\n'
I am using  $SHELL.

$ echo "I am using\t$SHELL.\n"
I am using\t/bin/bash.\n
$ echo $"I am using\t$SHELL.\n"
I am using\t/bin/bash.\n

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 \t or \n to variables. I could not find an application for the form $"...", though.

Dubu
  • 919
1
$ echo $'a\naa\nac\nb\ncc\tdd\ne\se'
a
aa
ac
b
cc  dd
e\se
$

In the above example, \n represents new line.For this Syntax $'code\ncode1', terminal first prints the code on first line and on the next line, it prints code1.

So in the example, terminal first start to executes the code.prints a on the 1st line, then it moves to the next character.The next character is \n which represents new line.So the it prints aa in new line.And the the bash interpreter executes like that.On seeing this code cc\tdd, it prints cc and after some space, it prints dd.Because \t represents tab.Next it prints e\se asusual because there is no value for \s.

$ echo $"a\naa\nac\nb\ncc\tdd\ne\se"
a\naa\nac\nb\ncc\tdd\ne\se
$

echo $"text".In this the echo command displays whatever given inside $"" as it as.It won't parse.

echo $'code' - It parses the code.

echo $"code" - If the code contains dollar variable,it should be expanded otherwise it won't for \n \t and prints all the other contents as it is.

Avinash Raj
  • 78,556
  • "It won't parse the code" is not correct. Variables in code will be expanded. Test echo $"$PWD". – Dubu Mar 11 '14 at 10:38
  • @Dubu i'm not talking about "$PWD", its $"PWD".Check echo $"PWD" – Avinash Raj Mar 11 '14 at 10:41
  • 2
    I could also have used $"Hello, I am in $PWD". You said "it won't parse the code", and I wanted to show that this is not correct. If you place something in code that contains dollar variables, they will be expanded. $"..." is mostly like "...", except that backslash escapes won't be parsed (with some exceptions like \$, \" etc.). But variables will be expanded. – Dubu Mar 11 '14 at 10:47
0

Check accepted answer in Differences between doublequotes " ", singlequotes ' ' and backticks ´ ´ on commandline?

For example purpose, consider that variable foo contains uname.

echo "$foo" outputs uname, subtitute variables in text.
echo '$foo' outputs $foo, the exact string (only ' should be escaped \').
echo `$foo` outputs Linux, execute the content of the variable and echo outputs it.

Here is also some nice Command Line Guides

Ahmadgeo
  • 1,401