- 論壇徽章:
- 0
|
- 空格
- 回車
- $
- #
- *
-
在unix系統(tǒng)中,許多特殊字符對shell來說都有特殊含義,例如,空格是命令和參數(shù)的分割符;剀嚂l(fā)送給shell執(zhí)行命令的信號,$符號被用來顯示與變量名相關(guān)聯(lián)的值。
在一些特殊的情況下,你不想要shell去解釋這些特殊字符的特殊含義。你只要求字面上的意義。因此,unix必須提供一種機制來忽略或消除一個指定的字符的含義。這種機制就叫做引用。
8.2 引用符號
反斜杠 \
單引號 ‘
雙引號 “
反斜杠消除緊跟在它后面的特殊字符的特殊意義。
單引號(’)會消除特殊字符的特殊含義。在單引號包圍之中的所有的特殊字符的特殊含義都會被忽略。單引號不能被忽略,它要被用來關(guān)閉被引用的字符串。
雙引號(”)的包容性要差一點,大多數(shù)的字符都可以使用雙引號來去除特殊含義。只有$符號(當其被用來作為變量和命令替代的時候),和反斜杠例外。你可以在雙引號中使用反斜杠來除掉 $號的特別含義
8.3 引用-- \
語法:
\ ??除去下一個字符的特別含義
例子:
$ echo the \ \ escapes the next character
the \ escapes the next character
$ color=red\ white\ and\ blue
$ ehco the value of \ $ colore is $color
the value of $colore is red white and blue
$ echo one two \
>three four
one two three four
反斜杠會忽略下一個特殊字符的特殊含義。沒有任何的例外。
8.4 引用-‘
語法:
‘ ???除去包括在單引號中的所有字符的特殊含義。
例子:
$ color =’red white and blue’
$ echo ‘the value of \$colore is $colore’
the value of $color is $color
$ echo ‘the value of $color is ‘ $color
the value of $color is red whie and blue
$ echo ‘this doesn’t work’
>ctrl + c
$ echo ‘***************”
*************
單引號會去除所有的包含在其中的特殊字符的特別含義。
8.5 引用 - “
“ ???去除包含在其中的特殊字符的特別含義,但\,$,和 “除外。
例子:
$ color = ‘red white and blue”
$ echo “the value of \$colore is $color”
the value of $volor is red white and blue
$ cur_dir=”$LOGNAME – your current directory is $(pwd)”
$ echo $cur_dir
user3 – your current directory is /home/user3/tree
$ echo “they’re all here ,\\ , ‘, \” “
they’re all here,/, ‘, “
雙引號引用不如單引號引用全面,大多數(shù)的特別字符都會被忽略,例外是你可以進行變量替代,$variable,和命令替代, $(cmd).
當你使用雙引號引用的時候,你也許會想要忽略這些特殊字符的含義。因而,反斜杠(\)同時也保持了它的特殊含義,可以用它來除去特殊字符$,或’的特殊含義。前提是他們出現(xiàn)在雙引號當中。
注意:引用機制只能在單個命令行中使用。
8.6 總結(jié)
機制 ??????目標
反斜杠 ?????忽略下一個字符
單引號 ?????忽略所有的在‘‘中的字符
雙引號 ?????忽略所有的在“”中的字符,除了 \, $,
????????{變量名}, 和$(comand)。
本文來自ChinaUnix博客,如果查看原文請點:http://blog.chinaunix.net/u2/84425/showart_2069315.html |
|