46

I don't do enough scripting to remember, without looking up, whether double or single quotes result in a Unix variable being substituted.

I definitely understand what is going on. My question is does anyone have a memory trick for making the correct quoting rule stick in my head?

peterh
  • 9,731
  • 2
    Use rc as your shell, then you have only one type of quotes (" is an ordinary character in rc where the only quotes are single quotes and backslash is only used for line-continuation (not inside single quotes)). It's also got a much leaner, cleaner, better, more intuitive syntax and design than Korn-like shells like bash. – Stéphane Chazelas Oct 26 '17 at 08:45
  • 2
    Learn Perl. Then you just have to memorize that shell behaves similar. – n0rd Oct 27 '17 at 16:06

7 Answers7

76

Single quotes are simple quotes, with a single standard: every character is literal.
Double quotes have a double standard: some characters are literal, others are still interpreted unless there's a backslash before them.

Single quotes work alone: backslash inside single quotes is not special.
Double quotes pair up with backslash: backslash inside double quotes makes the next character non-special.

diginoise
  • 103
7

Double-quotes (") are weak quotes- you need two of them, so they come in pairs, and variables punch right through them despite their numbers.

Single-quotes (') are strong quotes- they come singly, and variables are no match for them.

DopeGhoti
  • 76,081
6

"Double, double toil and trouble;
Dollars burn, and backquotes bubble."

Single, in contrast, is harmless.

3

Double quotes, double fun!
Parameters are expanded
One by one.

gboffi
  • 1,394
1

Double-quotes (") don't exactly mean what the say, for example a tool that says "quality" equipment is probably the opposite.

Single-quotes(') are strong quotes, what is between them is exactly what it seems.

Pelle
  • 401
1

Singular in purpose are they
Quotations that are single.

Treat literal the chars within
No matter how they mingle.

(T'is just one small rule you must know
Single quote within? No go!)


There are those quotes that are double
Inside which some chars may be trouble
Dollar, back-ticks and back-slash
Are treated special by Bash
Escaping, though, will burst that bubble.

B Layer
  • 5,171
1

One: winter, as is
Two: summer, transformations
Forbid or allow

drl
  • 838