How do you escape apostrophe in single quoted string in bash?

20

4

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

Here is an example:

$ 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?

Kibet

Posted 2012-10-01T10:04:03.573

Reputation: 338

echo \''Hello World'\' – math – 2012-11-13T08:11:54.637

Answers

21

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.

choroba

Posted 2012-10-01T10:04:03.573

Reputation: 14 741

3@choroba not "totally" true, in bash you can do echo $'\'hello world\'' – bufh – 2014-12-17T14:58:31.740

1You'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 – 2012-10-01T10:31:57.403

@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 – 2012-10-01T11:14:30.093

1@zero2cx: Not true: echo '\'' – choroba – 2014-03-31T21:00:56.680

@zero2cx: I would say "outside" instead of "inside". – choroba – 2014-04-01T16:14:56.553

11

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".)

chepner

Posted 2012-10-01T10:04:03.573

Reputation: 5 645

It's great! Thx – Mikl – 2020-01-21T15:43:32.940

4

Simple example of escaping quotes in shell:

$ echo 'abc'\''abc'
abc'abc
$ echo "abc"\""abc"
abc"abc

It's done by closing already opened one ('), placing escaped one (\') to print, then opening another one (').

Alternatively:

$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc

It's done by finishing already opened one ('), placing quote in another quote ("'"), then opening another one (').

What you did ('\'Hello World\''), is:

  1. Opened 1st apostrophe:'.
  2. Closed right after it \', so the string becomes: '\'.
  3. Hello World is not quotes.
  4. Placed standalone apostrophe (\') without opening it.
  5. Last apostrophe (') is opening string, but there is no closing one which is expected.

So the correct example would be:

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

Related: How to escape single-quotes within single-quoted strings?

kenorb

Posted 2012-10-01T10:04:03.573

Reputation: 16 795

3

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.

zero2cx

Posted 2012-10-01T10:04:03.573

Reputation: 611