部分特殊字符意义列表
Character | Where | Meaning |
<RETURN> | csh, sh | Execute command |
# | csh, sh, ASCII files | Start a comment |
<SPACE> | csh, sh | Argument separator |
` | csh, sh | Command substitution |
" | csh, sh | Weak Quotes |
' | csh, sh | Strong Quotes |
\ | csh, sh | Single Character Quote |
variable | sh, csh | Variable |
variable | csh, sh | Same as variable |
| | csh, sh | Pipe character |
^ | sh | Pipe Character |
& | csh, sh | Run program in background |
? | csh, sh | Match one character |
* | csh, sh | Match any number of characters |
; | csh, sh | Command separator |
;; | sh | End of Case statement |
~ | csh | Home Directory |
~user | csh | User's Home Directory |
! | csh | History of Commands |
- | Programs | Start of optional argument |
$# | csh, sh | Number of arguments to script |
$* | csh, sh | Arguments to script |
$@ | sh | Original arguments to script |
$- | sh | Flags passed to shell |
$? | sh | Status of previous command |
$$ | sh | Process identification number |
$! | sh | PID of last background job |
&& | sh | Short-circuit AND |
|| | sh | Short-circuit OR |
. | csh, sh | Typ. filename extension |
. | sh | Source a file and execute as command |
: | sh | Nothing command |
: | sh | Separates Values in environment variables |
: | csh | Variable modifier |
Character | Where | Meaning |
[ ] | csh, sh | Match range of characters |
[ ] | sh | Test |
%job | csh | Identifies job Number |
(cmd;cmd) | csh. sh | Runs cmd;cmd as a sub-shell |
{ } | csh | In-line expansions |
{cmd;cmd } | sh | Like (cmd;cmd ) without a subshell |
>file | csh, sh | Standard output |
>>file | csh, sh | Append to standard output |
<file | csh, sh | Standard Input |
<<word | csh, sh | Read until word, substitute variables |
<<\word | csh, sh | Read until word, no substitution |
<<-word | sh | Read until word, ignoring TABS |
>>!file | csh | Append to file, ignore error if not there |
>!file | csh | Output to new file, ignore error if not there |
>&file | csh | Send standard & error output to file |
<&digit | sh | Switch Standard Input to file |
<&- | sh | Close Standard Input |
>&digit | sh | Switch Standard Output to file |
>&- | sh | Close Standard Output |
digit1<&digit2 | sh | Connect digit2 to digit1 |
digit<&- | sh | Close file digit |
digit2>&digit1 | sh | Connect digit2 to digit1 |
digit>&- | sh | Close file digit |
键盘上有3种不同的引用标记。其中两种是英语中的引用,称作单引号和双引号。第三种引用标记是`(单词是backtick or grave)。前两种是用来引用词组或句子的在UNIX中,而"`"不是用来引用字符的,是用作命令替换的,在`与`之间的字符会作为命令进行执行并将结果插入当前行中。
Example:
$ echo the date is `date`
the date is Thu Dec 27 12:38:14 CST 2012
在UNIX中,有3中引用机制,分别是:',",\。
引用单个字符使用\
使用\可以阻止Shell解释\之后的字符。如删除一个文件:
[dave@localhost Documents]$ rm -v a\?
removed `a?'
这样就可以删除名称为a?的文件,如果不加\就会删掉aa,ab等类的文件。
单引号进行强引用
单引号之间的内容会保持文本原样,特殊字符也是如此:
[dave@localhost Documents]$ echo 'What the *heck* is a $ doing here???'
What the *heck* is a $ doing here???
双引号进行弱引用
使用双引号不会解释?和*等字符,但却会解释变量和命令替换,如:
[dave@localhost ~]$ echo "Is your home directory $HOME?"
Is your home directory /home/dave?
[dave@localhost ~]$ echo "Your current directory is `pwd`"
Your current directory is /home/dave
只要了解了'与"的区别,你就算是掌握了一个不错的技巧了,这个不难吧! '比"强,\是引用最强的一个。
嵌套引用
想使用单引号,就用双引号包围他。同样,使用双引号就用单引号包围他。如:
[dave@localhost ~]$ echo "Don't do that"
Don't do that
[dave@localhost ~]$ echo 'The quote of the day is: "TGIF"'
The quote of the day is: "TGIF"
在引号中使用相同的引号
有一个问题就是如何在同样的引号中使用自身。你或许以为可以像下面这样使用:
echo "The word for today is \"TGIF\""
echo 'Don\'t quote me'
第一个对Bourne shell有用, but not the C shell.第二个都不识别。这里我刚开始也有点搞不清,看哈下面这个例子:
echo 'a'b'c'
这个会被分成三个单元,第一个和最后一个是引用了的,中间的没有,在替换和引用被执行后,三个单元就会被连接起来,中间那个可以是个变量,如:
echo 'a'$HOME'b'
这是一种典型的办法把Shell变量放进AWK脚本中,下面是个演示:
#!/bin/sh
# this is a shell script that acts like a filter,
# but in only prints out one column.
# the value of the column is the argument
# to the script
#
# uncomment the next line to see how this works
#set -x
#
# example:
# printcol 1
# printcol 3
# the value of the argument is $1
# Here comes the tricky part -
awk '{print $'$1'}'
# I told you!
在这个例子中,Shell将给AWK的参数分割成三块了:
{print $ | Quoted |
$1 | Evaluated |
} | Quoted |
你可以将set -x的注释去掉,然后执行下面这个命令,看看结果:
[dave@localhost Documents]$ ./printcol 2 </etc/hosts
++ awk '{print $2}'
localhost.localdomain
localhost6.localdomain6
[dave@localhost Documents]$
因为\是最强的引用,当要在引号中使用相同的引号,使用\来进行引用是最好的做法:
[dave@localhost Documents]$ echo 'Don' \' 't do that'
Don ' t do that
我用空格将单引号隔开,就是想演示个单元之间的关系。
引用较长的行
在C SHELL中引用跨行需要在行尾加一个\,而B SHELL则不用,如:
[dave@localhost Documents]$ echo "this is a
> long line"
this is a
long line
[dave@localhost Documents]$ echo this is a \
> long line
this is a long line
[dave@localhost Documents]$