Bash How to Escape Single Quote

Bash How to Escape Single Quote

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

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

Output: $foo

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

echo \'$foo\'

Output: ‘Hello world’

In single quotes, escaping a single quote is not possible, thus you can’t include a 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.

Leave a Comment