Preventing Expansion

Preventing Expansion

  • Backslash ( \ ) makes the next character literal
    $ echo your cost: \$5.00
    your cost: $5.00
  • Quoting prevents expansion
    • Single quotes(‘) inhibit all expansion
    • Double quotes(“) inhibit all expansion, except:
      • $(dollar sign) – variable expansion
      • ` (backquotes) – command substitution
      • \(backslash) – single character inhibition
      • !(exclamation point) – history subsitution

Escaping characters
Sometimes the fact that bash treats some characters specially can cause problems. Consider the following command:
[user@stationX ~]$ echo *** WARNING ***

In this case the * is expanded by the shell. If there are any files in the current directory, each set of *** will be replaced with the names of those files, and the output of the echo command will not be as-intended. Try this in a populated directory to see for yourself. Note that this problem applies to any shell character, including !, $, ? and ohers, not just to *.

bash can be told to send a literal character, with no special interpretation, by escaping the character. The simplest way to escape a character is by preceding it with \. for example, the following would reliably print *** WARNING ***:

[user@stationX ~]$ echo \*\*\* WARNING \*\*\*

Inhibiting expansion with quotes

Escaping large numbers of individual characters can be tedious. Not only is it displeasing to the eye, it also invites typing errors. To inhibit multiple characters from being expanded it is useful to use eighter single or double quotes:

[user@stationX ~]$ echo ‘*** WARNING ***’

The difference between using single and double quotes is that single quotes will inhibit just about all command line expansion, whereas double quotes will inhibit some expansion, but allow others. Asar rule of thumb,

Leave a Comment

%d bloggers like this: