pavement

Escape

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
 
(added examples, minor shell session reformatting, a little rewording, added category)
Line 1: Line 1:
The escape character (\) is used to "escape" special characters.
+
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, to cause normally non-special characters ''to'' perform special functions.
  
Special characters are those which have a reserved meaning on UNIX like OSs.  
+
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.
  
An example is the space character. 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 seperator between file names, or between switches.
+
If during a shell session you typed:
  
For example if you typed
+
# '''emacs my file.txt'''
  
emacs my file.txt
+
emacs would try to open the files '''my''' and '''file.txt'''.
  
emacs would try to open the files "my" and "file.txt".
+
but if you escaped the space character:
  
but if you escaped the space character
+
# '''emacs my\ file.txt'''
  
emacs my\ file.txt
+
then emacs would try to open the file '''my file.txt'''.
  
then emacs would try to open the file "my file.txt".
+
Conversely, '''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'''.
 +
 
 +
[[Category:FreeBSD Terminology]]

Revision as of 10:53, 28 January 2005

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, 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.

If during a shell session you typed:

# emacs my file.txt

emacs would try to open the files my and file.txt.

but if you escaped the space character:

# emacs my\ file.txt

then emacs would try to open the file my file.txt.

Conversely, 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.

Personal tools