Examples of quoting special characters

To remove the special meaning from a single metacharacter:

   rcp ubik:/home/roger/recipe.ps .;\\<Return>
   lpr -Pps3 #6m recipe.ps

This prevents the shell from expanding the newline character generated by pressing the <Return> key. You can then continue entering the command on the next line.


To prevent the shell from expanding an argument containing several special characters:

   $ sed -e 1,$s/V[0-9].*: //p /etc/motd
   sed: command garbled: 1,/V[0-9].*:
   $ sed -e '1,$s/V[0-9].*: //p' /etc/motd
   DYNIX/ptx(R) Tue Feb 25 12:04:15 GMT 1993

First, the shell tries to expand the command line and changes its meaning in the process. When the arguments to the command are protected by enclosing them in single quotes the sed command is successful. It displays the operating system version number contained in the message of the day.


To prevent the shell from expanding special characters but allow command substitution:

   echo 'My working directory is `pwd`'
   My working directory is`pwd`
   echo "My working directory is `pwd`"
   My working directory is /home/jane/docs

When enclosed within single quotes the characters `pwd` are treated literally as part of the string to be echoed. Placed within double quotes, the command within the backquotes is executed and its output substituted in its place.


[Home] [Search] [Index] This site maintained by staff@stacken.kth.se