Quotes and Escaping
Spaces and Other Special Characters
If you have a file or folder name containing spaces or brackets, for example, you may notice it doesn't work right with command line commands, like ls. This is because spaces are used to separate parts of a command, so they have to be entered specially when they represent just the character. This is termed escaping and takes different forms in different contexts and programming languages. For the Linux command line, this is done by preceding the relevant character by a \.
Note that some characters have additional special meanings with the \, such as '\n' which means newline, and '\t' which means Tab.
Note that escaping is not the same as encoding. For example, the Wikipedia page https://en.wikipedia.org/wiki/Escape_character replaces the ' ' with an '_', and you may have seen '+' or '%20' used this way too.
The alternative to escaping a charcter is enclosing the entire string in quotes. Single ' and double " quotes have slightly different behaviours. Single quotes preserve every character within them. Double quotes preserve all except $, / and `. This is illustrated by running the following:
echo Hi $USER,
echo Hi \$USER,
echo "Hi $USER",
echo 'Hi $USER'
Note that the $USER is an environment variable. See The Shell Environment for more on these.
List of Characters that need Escaping
In the shell, the following characters always need either escaping or putting in quotes. This is not a complete list!
- ( )
- | <> (the pipe characters)
- & (run process in the background)
- ! (only if bash history is enabled, which is the case on RTPSC machines)
- ' or " (Note the ' cannot be used inside paired '', even with a \ )
The following characters are not escaped by double quotes alone: