Escaping

From LQWiki
Jump to navigation Jump to search

Escaping a character usually means removing the special meaning that it conveys, although it can also give it meaning. It is commonly used inside strings when the quotation mark that would indicate the close of the string must be included literally. Other uses include at the end of a line to indicate that it runs into the next (used by many programming and scripting languages, including C/C++ and Bash).

The most common means of escaping is preceeding a character with a backslash (which can itself be escaped in the same way if a backslash is needed at the end of a string), although doubling the character to be escaped is also used.

Example

In the bash shell you can put a string in apostrophes:

# echo "hello world"
hello world

In order to put apostrophes into that string, you need to escape them:

# echo "hello \"world\""
hello "world"