9

I'm used to type strings with quotation marks, but from now and then when I surf through config files and recommendations, I see apostrophes being used.

I often copy apostrophe things into my config files and substitute it with my quotation marks so that the code fits together.

But what is their difference? (I mostly target this for bash. If other Linux-based scripting does it different, that would be an extra.)

For example:

echo "Hello World!" and echo 'Hello World!'

Has it something to do that I maybe can't use (formatted) variables if I use one or the other, or is it simply personal preference?

Trollwut
  • 481

3 Answers3

15

There is a difference between single and double quotes, and it's not a matter of style. Single quotes will literally echo what you write, so you can be sure that what you write is what it'll be printed. On the other hand, double quotes will interpolate variables' value.

From man bash:

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, backtick, \, and, when history expansion is enabled, !. The characters $ and backtick retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

So in your example:

#!/bin/bash
PLACE="World"
echo "Hello $PLACE !"
echo 'Hello $PLACE !'

will result in this output:

Hello World !
Hello $PLACE !
dr_
  • 29,602
  • 2
    This isn't terribly relevant, but I'd like to point out that in many languages with single and double quoted strings, if there is a difference, then this is usually it. Double quoted strings usually allow some form of expansion while single quoted strings usually do not. – Captain Man Feb 10 '17 at 20:19
  • @CaptainMan, and the big difference between these languages is if you can escape a single quote with a backslash. – Mark Feb 11 '17 at 00:26
5

https://www.gnu.org/software/bash/manual/bashref.html#Quoting

Inside double quotes variables are expanded.

Single quotes prevent string interpolation:

Enclosing characters in single quotes preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

There’s also the notation $'…' which allows allows for ANSI C escape sequences.

phg
  • 1,817
0

This depends on the language. You mentioned bash, and the other answers have concentrated on that, but the way you phrased your question suggests that you think there is a general rule here. There isn't.

For example, thinking of a few random languages which come to mind:

  • In bash (and other sh-like shells), "..." expands variables within the string, and '...' doesn't. Heaven knows what csh does.
  • In Python, the two are equivalent, and you can choose between them pretty much on the basis of taste or convenience.
  • In C-like languages, "..." is a string, 'x' is a single character, and 'abc' is a syntax error.
  • In lisps, "..." is a string, and ' ‘quotes’ whatever comes after it.
  • In a configuration file, the ‘correct’ quotes may be `...' or '...' or "..." or [...] or |...| or whatever the author of the program being configured thought would be a nifty idea.
  • In Postscript, strings are marked by (...).
  • In (slightly antique) Fortran, a 5-character string ‘hello’ can be written as 5Hhello (that is, this is an example of strings marked with nothing resembling quotes).
  • In English they nest ‘I said “hello” to him’ or “I said ‘hello’ to him” depending on which side of the Atlantic your editor lives on.
  • I'm sure one could think of a few even more arcane examples, given time.

Of those, only the Python one is a matter of taste or style. Quotes are just another bit of language syntax.