The shell provides a mechanism called quoting to selectively suppress unwanted expansions.
Double Quotes
作用
除了以下的特殊字符,其他特殊字符都会被当作一般字符处理:
$dollar sign: parameter expansion, arithmetic expansion\backslash: escape- ``` 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
- Word splitting for newlines is suppressed by double quotes.```bash$ echo $(cal)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$ echo "$(cal)"February 2020Su Mo Tu We Th Fr Sa12 3 4 5 6 7 89 10 11 12 13 14 1516 17 18 19 20 21 2223 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'
