pavement

Escape

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(added examples, minor shell session reformatting, a little rewording, added category)
m (REALLY minor formatting change)
Line 3: Line 3:
 
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.
 
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:
+
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 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''', which is not what you wanted. 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 open '''my file.txt''' as you intended.
  
 
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'''.
 
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]]
 
[[Category:FreeBSD Terminology]]

Revision as of 10:56, 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.

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