The shell provides a mechanism called quoting to selectively suppress unwanted expansions.

Double Quotes

作用

除了以下的特殊字符,其他特殊字符都会被当作一般字符处理:

  1. $ dollar sign: parameter expansion, arithmetic expansion
  2. \ backslash: escape
  3. ``` backtick or backquote: command substitution

    Examples

  • Word splitting for spaces is suppressed by double quotes. ```bash $ echo this is a test this is a test

$ echo “this is a test” this is a test

  1. - Word splitting for newlines is suppressed by double quotes.
  2. ```bash
  3. $ echo $(cal)
  4. Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  5. $ echo "$(cal)"
  6. February 2020
  7. Su Mo Tu We Th Fr Sa
  8. 1
  9. 2 3 4 5 6 7 8
  10. 9 10 11 12 13 14 15
  11. 16 17 18 19 20 21 22
  12. 23 24 25 26 27 28 29

Single Quotes

作用

Single quotes suppress all expansions, even escape character().

Examples

 echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER \n'
 text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER \n

Escaping Characters

作用

Escape character suppresses only a _single _character.

Examples

$ echo "The balance for user $USER is: \$5.00"
The balance for user ronnie is: $5.00

$ mv bad[\&\$\!]filename good_filename

$ echo \\ # escape a escape character.
\

Backslash Escape Sequences

Escape sequence Meaning
\a Bell (an alert that causes the computer to beep)
\b Backspace
\n Newline; on Unix-like systems, this produces a line feed
\r Carriage return
\t Tab
$ sleep 10; echo -e "Time's up\a"  # -e enable interpretation of escape sequences.

$ sleep 10; echo "Time's up" $'\a'