47

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?

2
  • 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. Commented Oct 26, 2017 at 8:45
  • 2
    Learn Perl. Then you just have to memorize that shell behaves similar. Commented Oct 27, 2017 at 16:06

7 Answers 7

79

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.

4
  • 7
    This is quite poetic. With a good editor (in the literary sense), we could make a children's book out of this. "If You Teach a Snake to Script" Commented Oct 25, 2017 at 20:21
  • 7
    I've been trying to turn Single quotes are simple quotes, with a single standard into a Dr. Seuss style poem for twenty minutes, and failing miserably. Commented Oct 25, 2017 at 21:58
  • 7
    Single quotes are single use, and lit'rally repeat / Double quotes are more verbose, a paramat'rising feat Commented Oct 26, 2017 at 16:40
  • @glennjackman A FORTRAN colouring book Commented Oct 27, 2017 at 13:18
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.

0
6

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

Single, in contrast, is harmless.

0
3

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

1
  • If you write 'Params expanded one by one' it fixes the meter. Commented Oct 18, 2022 at 10:12
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.

1

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

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.