Friday, January 29, 2010

Passing Special Characters to Commands

Citing from The Linux Cookbook: Tips and Techniques for Everyday Use

Some characters are reserved and have special meaning to the shell on their own. Before you can pass one of these characters to a command, you must quote it by enclosing the entire argument in single quotes (`'').

For example, here's how to pass `Please Stop!' to a command:

'Please Stop!'
When the argument you want to pass has one or more single quote characters in it, enclose it in double quotes, like so:

"Please Don't Stop!"
To pass special characters as a string, give them as:

$'string'
where string is the string of characters to be passed. Special backslash escape sequences for certain characters are commonly included in a string, as listed in the following table.
ESCAPE SEQUENCE DESCRIPTION
\a Alert (rings the system bell).
\b Backspace.
\e Escape.
\f Form feed.
\n Newline.
\r Carriage return.
\t Horizontal tab.
\v Vertical tab.
\\ Backslash.
\NNN Character whose ASCII code is NNN in octal (base 8).
To demonstrate the passing of special character sequences to tool, the following examples will use the figlet tool, which displays the text you give as an argument in a "font" made up of text characters (see Horizontal Text Fonts).

To pass a backslash character as an argument to figlet, type:
$ figlet $'\\' [RET]
To pass a form feed character followed by a pilcrow sign character (octal character code 266) to figlet, type:
$ echo $'\f\266' [RET]

No comments: