Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I don't understand how Bash evaluates escaping of apostrophe characters in single quoted strings.

$ echo ''\''Hello World'\'''
'Hello World' #works

$ echo '\'Hello World\''
 > # expects you to continue input

I've tried looking for explanations to this but couldn't get anything. What is Bash doing here?

share|improve this question
    
echo \''Hello World'\' –  math Nov 13 '12 at 8:11
add comment

3 Answers

up vote 16 down vote accepted

In single quotes, no escaping is possible. There is no way how to include a single quote into single quotes. See Quoting in man bash.

share|improve this answer
    
You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts. –  Kibet Oct 1 '12 at 10:31
    
@Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics. –  zero2cx Oct 1 '12 at 11:14
    
Thank you zero2cx –  Kibet Oct 1 '12 at 11:18
1  
@zero2cx: Not true: echo '\'' –  choroba Mar 31 at 21:00
    
@zero2cx: I would say "outside" instead of "inside". –  choroba Apr 1 at 16:14
add comment

To explain what is happening with your escaped apostrophes, we'll examine your second example (also see single quotes, or strong quotes):

$ echo '\'Hello World\''
>     # expects you to continue input

Here, you've left the quotation hanging, as you've stated. Now trim the end and change it to:

                     v                                v           v
$ echo '\'Hello World     # Echo two strings: '\' and 'Hello World'.
\Hello World         ^

The "Hello World" sub-string wasn't quoted here, but it behaved as if it was strong quoted. Using your example again, trim the end differently this time:

                     vv                                    v (plain apostrophe)
$ echo '\'Hello World\'   # Will echo: '\' and 'Hello World''
\Hello World'        ^^   # Note that the trailing ' char is backslash escaped. 

The "Hello World" sub-string again behaves as if it were strong quoted, with only the added apostrophe (escaped, so no longer a single quote) at the end.

When another single quote is added to the end (your original example) the string is left hanging and waiting for a close-quote.

share|improve this answer
add comment

In addition to POSIX-supported single- and double-quoting, bash supplies an additional type of quoting to allow a small class of escaped characters (including a single quote) in a quoted string:

$ echo $'\'Hello World\''
'Hello World'

See the QUOTING section in the bash man page, near the end of the section. (Search for "ANSI C".)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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