How-to: Use Escape Characters, delimiters and Quotes


Quoting is used to remove the special meaning of certain characters or words to the shell. Quoting can be used to disable special treatment for special characters, to prevent reserved words from being recognized as such, and to prevent parameter expansion.

Whenever you pass a variable to a command, you should probably quote it.

Each of the shell metacharacters has special meaning to the shell and must be quoted if it is to represent itself.

Escape Character

A non-quoted backslash \ is the Bash escape character. It preserves the literal value of the next character that follows, with the exception of newline. If a \newline pair appears, and the backslash itself is not quoted, the \newline is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).

Single Quotes

Enclosing characters in single quotes (') preserves the literal value of every character within the quotes. A single quote can not occur between single quotes, even when preceded by a backslash.
$ MONTHVAR=January
$ echo 'The month is $MONTHVAR'
The month is $MONTHVAR

Double Quotes

Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, ', and \ and, when history expansion is enabled, !. The characters $ and ' retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, ', ", \, or newline.
If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.
Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote can be quoted within double quotes by preceding it with a backslash.
The special parameters * and @ have special meaning when in double quotes.
$ MONTHVAR=January
$ echo "The month is $MONTHVAR"
The month is January

EOF Marker

End Of File is usually CTRL+D (^D) when input is from the keyboard.
If ^D doesn’t work, type 'stty -a' to see what the eof character is.

ANSI-C Quoting

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specifed by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

 \a   Alert (bell) 
 \b   Backspace 
 \e   An escape character (not ANSI C) 
 \f   Form feed 
 \n   Newline 
 \r   Carriage return 
 \t   Horizontal tab 
 \v   Vertical tab 
 \\   Backslash 
 \'   Single quote 
 \nnn The character whose ASCII code is the octal value nnn 
     (one to three digits) 
     The character whose ASCII code is the hexadecimal value nnn 
     (one to three digits) 

The expanded result is single-quoted, as if the dollar sign had not been present.

Locale-Specific Translation

A double-quoted string preceded by a dollar sign ($) will cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored. If the string is translated and replaced, the replacement is double-quoted.

Line Continuation

The \ is used to continue a command on a separate line.

The \ must be followed directly by a return. There can be NO SPACE.

The line> is the secondary prompt issued by the shell for line continuation.

Related Linux commands - quoting in shell programming.
Windows equivalent: Escape characters

Copyright © 1999-2024
Some rights reserved