bash how to escape single quote

Answer

Enclosing characters or variable in single quotes (') represents the literal value of characters. Example:

$foo="Hello world" 
echo '$foo'

Output: $foo

If you want to write content of variable $foo in ' use the character \ to escape single quote.

echo \'$foo\'

Output: 'Hello world'

In single quotes, escaping single quote is not possible, thus you can't include single quote into single quotes

Instead of single quotes use double quotes ("):

echo "Hello'world"

Output: Hello'world

Single quote may not occur between single quotes, even when preceded by a backslash, but this works:

echo $'I\'m a linux admin.'

Output: I'm a linux admin.

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.