pavement

Escape

From FreeBSDwiki
Jump to: navigation, search

In unix-like systems, the escape character is not what you get when you press the escape key - it is the backslash (\), which is used to "escape" special characters when you don't want those special characters to perform their special functions - or, in some cases in Perl, to cause normally non-special characters to perform special functions.

The space character is an excellent example of a special character you may want to escape. If you want access a file that has a name with a space in it you need to escape this character, to ensure that it is not treated as a separator between file names, or between command line switches.

Let's say that you want to edit a file named my file.txt. If during a shell session you typed:

# emacs my file.txt

emacs would try to open the files my and file.txt, which is not what you wanted. But if you escaped the space character:

# emacs my\ file.txt

then emacs would open my file.txt as you intended.

Conversely, if you're writing a Perl script, n is an example of a normally non-special character which becomes special when escaped. If you want to echo carriage returns to standard output or to a file, you can't do it by pressing enter - that just executes the echo command (or begins another line in your script, if you're programming). What you need to do is use \n - because when you escape the n character, what you're telling the system is that you want a newline.

#!/usr/bin/perl

print `echo "Look ma - blank spaces!\n\n\n"`;

NOTE: the "newline" character is not applicable to shell sessions or shell programming, whereas escaping spaces and other special characters is.

Personal tools