UNIX SHELL学习Day1——Quote

部分特殊字符意义列表


CharacterWhereMeaning
<RETURN>csh, shExecute command
#csh, sh, ASCII filesStart a comment
<SPACE>csh, shArgument separator
`csh, shCommand substitution
"csh, shWeak Quotes
'csh, shStrong Quotes
\csh, shSingle Character Quote
variablesh, cshVariable
variablecsh, shSame as variable
|csh, shPipe character
^shPipe Character
&csh, shRun program in background
?csh, shMatch one character
*csh, shMatch any number of characters
;csh, shCommand separator
;;shEnd of Case statement
~cshHome Directory
~usercshUser's Home Directory
!cshHistory of Commands
-ProgramsStart of optional argument
$#csh, shNumber of arguments to script
$*csh, shArguments to script
$@shOriginal arguments to script
$-shFlags passed to shell
$?shStatus of previous command
$$shProcess identification number
$!shPID of last background job
&&shShort-circuit AND
||shShort-circuit OR
.csh, shTyp. filename extension
.shSource a file and execute as command
:shNothing command
:shSeparates Values in environment variables
:cshVariable modifier
CharacterWhereMeaning
[ ]csh, shMatch range of characters
[ ]shTest
%jobcshIdentifies job Number
(cmd;cmd)csh. shRuns cmd;cmd as a sub-shell
{ }cshIn-line expansions
{cmd;cmd }shLike (cmd;cmd ) without a subshell
>filecsh, shStandard output
>>filecsh, shAppend to standard output
<filecsh, shStandard Input
<<wordcsh, shRead until word, substitute variables
<<\wordcsh, shRead until word, no substitution
<<-wordshRead until word, ignoring TABS
>>!filecshAppend to file, ignore error if not there
>!filecshOutput to new file, ignore error if not there
>&filecshSend standard & error output to file
<&digitshSwitch Standard Input to file
<&-shClose Standard Input
>&digitshSwitch Standard Output to file
>&-shClose Standard Output
digit1<&digit2shConnect digit2 to digit1
digit<&-shClose file digit
digit2>&digit1shConnect digit2 to digit1
digit>&-shClose 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
$1Evaluated
}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]$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值