I needed to retain the double quotes around a string variable defined in bash in order to be able to pass it to a dialect of the Scheme programming language. My question is why are the double quotes retained when placed inside another set of single quotes? In order to illustrate this I give some examples from my bash command prompt:

$ str1=hey
$ echo $str1
$ str2="hey"
$ echo $str2
$ str3='hey'
$ echo $str3
$ str4='"hey"'
$ echo $str4
$ str5="'hey'"
$ echo $str5

A single-quoted string will retain the string as a literal. A double-quoted string will retain the string with variable interpolation and expansion. This is explained in the bash man page - see the section titled QUOTING

There are three quoting mechanisms: the escape character, single quotes, and double quotes.

A non-quoted backslash (\) is the escape character. It preserves the literal value of the next character that follows, with the exception of {newline}. If a \{newline} pair appears, and the backslash is not itself quoted, the \{newline} is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

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 $, `, \, and, when history expansion is enabled, !. The characters $ and ` 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.

  • Actually I am a bit rusty on the concept of strings, can you point me to a good source? For example I am not exactly sure what does string literal mean. Furthermore, I did not understand the exceptional case when there is a newline pair, can you elaborate on that a little please? – Vesnog Feb 6 '15 at 11:10
  • @Vesnog a string literal is simply a string that is printed, assigned, and used as a sequence of characters ("literally as written"). A=fish; B='$A car' would assign six characters to B, starting with a dollar sign. A=fish; B="$A car" would assign eight characters to B, starting with the letter f. – roaima Feb 6 '15 at 13:02

See the section on quoting in the Bash Reference Manual.

Basically, enclosing characters within single or double quotes turns them into literal characters, with no special meaning (there are a few exception for double quotes but they don't matter here). So in the character sequence '"hey"', the single quotes "protect" all the other characters, and the double quotes lose their special meaning and are preserved as literal characters.

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.