How do you escape apostrophe in single quoted string in bash?
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.
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".)
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:
- Opened 1st apostrophe:
'
. - Closed right after it
\'
, so the string becomes:'\'
. Hello World
is not quotes.- Placed standalone apostrophe (
\'
) without opening it. - 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?