HOWTO’s

Metacharacters

I put this together for my own purposes. Maybe someone out there will find it useful. By the way, all examples were tested on a Sparc5 and a 233 MHz Pentium running Solaris8. Your actual gas mileage may vary!

Quotes, Escape Characters, and Command Substitution

The UNIX escape characters are:

  • The single quote ‘
  • The double quote “
  • The back slash \
  • The backward single quote (a.k.a. backtick, `) is related; it is used for command substitution.

    Which To Use and When

    Each has slightly different characteristics but in general one could say the backslash is the strongest, single quote is in the middle and double quotes are kind of wimpy. These are called escape characters because they cause special characters to escape their special meaning and be taken literally by the kernel. They are not interpreted by the shell.

  • All special characters are escaped by single quotes
  • All special characters are escaped by the backslash
  • All special characters are escaped by double quotes except backticks, backslashes, and dollar signs
  • Preserving Spaces

    All three will work to preserve spaces:

    Example 1:
    $ echo I want three spaces here
    I want three spaces here

    Example 2:
    $ echo “I want five spaces here”
    I want three spaces here

    Example 3:
    $ echo ‘I want five spaces here’
    I want five spaces here

    Example 4:
    $ echo I want five spaces \ \ \ \
    here
    I want five spaces here

    In example 1 the spaces were removed by the shell. In examples 2-4 the quotes preserved the spaces. So all three worked fine but really, do you want to type backslash space, backslash space, backslash space, backslash space (example 4)?

    Escaping Metacharacters
    The double quote will ignore backticks, backslashes, and dollar signs but will escape all other metacharacters. So if you want to echo the string The $sign is used for environment variables, you could not use the double quotes:

    Example 5:
    $ echo “The $sign is used for environment variables”
    The

    is used for environment variables

    Notice the shell replaced $sign with its value (nothing) in the result.

    Example 6:
    $ echo ‘The $sign is used for environment variables’
    The $sign is used for environment variables

    So we can use the single quotes in this case. And also the backslash –

    Example 7:
    $ echo The \$sign is used for environment variables

    The $sign is used for environment variables

    Marvelous! But wait a minute, what if we want to echo a line with the environment variable in it and four spaces between the variable and the sentence?

    Example 8:
    $ echo The PATH environment variable is $PATH
    The PATH environment variable is /usr/bin:/bin:/usr/pkg/bin:/usr/local/bin

    That didn’t work, the spaces are gone! Double quotes will work perfectly –

    Example 9:
    $ echo “The PATH environment variable is $PATH”

    The PATH environment variable is /usr/bin:/bin:/usr/pkg/bin:/usr/local/bin

    Of course you could use the backslash –

    Example 10:
    $ echo The PATH environment variable is \ \ \ $PATH
    The PATH environment variable is /usr/bin:/bin:/usr/pkg/bin:/usr/local/bin

    The NewLine Character

    You don’t see the newline character at the end of each line but it must be there. You can use quotes to ignore or preserve the newline character –

    Example 11:
    $ STUFF=”first line

    second line”

    $ echo $STUFF

    first line second line

    $ echo “$STUFF”

    first line
    second line

    You can rid file names of characters with special meanings. For example, how do you delete a file named * in a directory of other files? If you type rm * the shell will do just that, delete everything in the directory! Using an escape character getting rid of or renaming that file is easy –

    Examples 12:
    $ rm “*”
    $ rm ‘*’
    $ rm \*
    $ mv ‘*’
    file4

    Echoing Special Characters

    Sometimes you will want a character in a report or script output that has special meaning. You may want to echo What is your ID number? to the terminal. But it’s not as easy as you first think!

    Example 13:
    $ echo What is your ID number?

    echo: No
    match.

    Easy enough, just escape the question mark –

    Example 14:
    $ echo What is your ID number\?
    $ echo “What is your ID number?”
    $ echo ‘What is your ID number?’

    Here are all the special characters I could think of –

    Example 15:
    $ echo
    \*\^\$\\\[\]\(\)\{\}\<\>\+\-\?\.\|\”\’\|\~\`

    *^$\[](){}<>+-?.|”‘|~`

    Did I miss anything?

    Checking Yourself

    If you need to run a command using special characters and you are not sure of yourself it is a good idea to test your syntax on a non-destructive example first. For instance, if you want to delete all files starting with xyz but no others try using your syntax with the ls command first to see if it gives you the results you are after –

    Example 16:
    $ ls xyz*
    $ rm xyz*

    Echoing ascii

    Every now and then you may need to use ascii to represent a specific character. You can use echo to insert the character (this is very shell and operating system specific) –

    Example 17:
    $ echo
    ‘\0nnn’

    Cool beans!