UNIX SHELL学习Day1——Quote
来源:互联网 发布:java 全角半角 空格 编辑:程序博客网 时间:2024/06/05 16:07
部分特殊字符意义列表
键盘上有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.localdomainlocalhost6.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 linethis is a long line[dave@localhost Documents]$
- UNIX SHELL学习Day1——Quote
- Html学习—Day1
- python学习——Day1
- UNIX Shell学习Day2——Regular Expressions
- Linux 与 unix shell编程指南——学习笔记
- 学习UNIX shell 吧!
- Unix Shell学习杂记
- Unix shell学习笔记
- 学习Unix Shell常用命令
- Mysql学习之路——day1
- 系统学习bootstrap——day1
- Java基础学习——day1
- IMWeb前端学习笔记——Day1
- Kotlin学习笔记——Day1
- LoadRunner学习笔记——Day1
- 《unix环境高级编程》之学习笔记--day1
- 《Shell Programming in Unix, Linux and OS X, 4th Edition 》 笔记之Can I Quote You On That?
- Unix Shell学习笔记【1】
- NSDate,NSString相互转换,解决八小时间隔问题
- Sublime Text 2
- 消息队列-zmq常用通信模式
- Kinect的“心脏”—PS1080 SoC
- 在工具栏创建组合框
- UNIX SHELL学习Day1——Quote
- 自我驱动
- "Gdiplus::GdiplusBase::operator new 函数不接受3 个参数"问题的处理
- [转] opencv 裁剪图片
- 大数相乘
- 爬网设置
- C#中的委托和事件
- 利用fsutil命令创建指定大小的测试文件
- UTF8 CString 互转问题和代码