43

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
    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 8:45
  • 1
    Learn Perl. Then you just have to memorize that shell behaves similar. – n0rd Oct 27 '17 at 16:06
74

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.

  • 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" – glenn jackman Oct 25 '17 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. – Ti Strga Oct 25 '17 at 21:58
  • 6
    Single quotes are single use, and lit'rally repeat / Double quotes are more verbose, a paramat'rising feat – DopeGhoti Oct 26 '17 at 16:40
  • @glennjackman A FORTRAN colouring book – Muzer Oct 27 '17 at 13:18
5

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

Single, in contrast, is harmless.

4

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.

2

The double quotes double the fun
expanding parameters one by one.

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

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.