Escape
|  (added examples, minor shell session reformatting, a little rewording, added category) |  (wups, \n isn't applicable to shell sessions) | ||
| (One intermediate revision by one user not shown) | |||
| Line 1: | Line 1: | ||
| − | 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. | + | 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. | 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: | 
| − | + | ||
| − | + | ||
|   # '''emacs my\ file.txt''' |   # '''emacs my\ file.txt''' | ||
| − | then emacs would  | + | 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. | |
| [[Category:FreeBSD Terminology]] | [[Category:FreeBSD Terminology]] | ||
Latest revision as of 11:07, 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 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.
