Home‎ > ‎CIS 98‎ > ‎

Escaping and Quoting

  1. Shell Metacharacters
    The shell uses many characters to denote some special meaning. e.g. * < > $ ; You might have occasions when entering a command line, whether interactively or in a shell script where you do not want that special meaning. The shell provides a way, in fact, three ways - to neutralize the special meaning: single quotes, double quotes, and the blackslash (\).
    1. Shell Metacharacters
      1. #
      2. $
      3. | and ^
      4. < and >
      5. *, ? and []
      6. () and {}
      7. ' " \
      8. `
      9. ;
      10. tab and space
      11. newline
      Note that . and / are not shell metacharacters;
      they do have special meaning to the kernel.
    2. Quoting Characters
      1. Single Quotes
        Single quotes are used in pairs to remove the special meaning of all enclosed metacharacters with the exception of other single quotes.
      2. Double Quotes
        Like single quotes except, the followig characters retain their special meaning:
        1. $ and $(cmd)
        2. \
        3. Other double quote characters
        Note: It is frequently a good idea to enclose a variable within double quotes on a command line so its value does not get interpreted by the shell! 
      3. Backslash
        The backslash (\) removes the special meaning of the one shell metacharacter that follows it. It's almost the same as placing single quotes around the metacharacter.
        • Single quotes around a newline metacharacter tells the shell to treat it as a literal character.
        • A backslash before a newline tells the shell to ignore the character, so that the line may be continued onto the next line.
        • The backslash retains its meaning inside double quotes if the character following has special meaning and can be neutralized; otherwise double quotes also neutralizes the backslash character.

  2. Special Shell Variables
    $0     Command name
    PID of current shell
    PID of last background process

    1. Positional Parameters
      A shell script, or a shell function, accesses the command line arguments via the Positional Parameters:
      • $#   - the number of arguments on the command-line not counting the name of the command.
      • $0   - the name of the program as it was invoked.
      • $1   - the first argument/option on the command-line
      • $2   - the second positional parameter
      • $3   - the third positional parameter
      • $*   - all the positional parameters together
      • "$@"   - each positional parameter separately quoted

      The shift statement causes postitional parameters 1 through n to shift down one position. $# is also decremented by this operation.

      The set command sets the positional parameters of the current shell to the arguments on the comannd line.

Exercise 1: Using Double Quotes

With the variable text set to the string, "* means all files in the directory", what will the following command output to the screen? How can you get what you want?
	$ echo $text

Exercise 2: Using Quotes

Use one echo command to display this message to stdout:
 John asked, "What's A:\setup.exe on this disk?"

Exercise 3: Passing Arguments to Programs

Write a one line script called ison which takes one argument: the name of a user. The ison program should output all instances of that user currently logged on according to the who command. If the user is not logged on, there should be no output.