SHELL的单引号'',双引号"",及$字符单双引号及字符转义的小结
2010-07-22 13:39
423 查看
在shell运行命令中,单双引号总能引起或多或少的话题讨论,这边做个小总结
先看以下例子:
实际上,分析单双引号,要从二个层面上去考虑以上边的例子:
第一层:shell传递参数给echo前进行的扩展解析
第二层:echo接受参数,处理,显示结果
具体分析如下:
第一层:shell传递参数给echo前进行的扩展解析
单引号:shell单引号不进行字符转义
原文:Enclosing characters in single quotes preserves the literal value of each character within the quotes.
双引号:不进行字符转义,除了三个字符$, `, /,和开启命令扩展后的!字符
$加单引号(即$''):除了以下字符转义,其他同单引号
/a alert (bell)
/b backspace
/e an escape character
/f form feed
/n new line
/r carriage return
/t horizontal tab
/v vertical tab
// backslash
/' single quote
/nnn the eight-bit character whose value is the octal value nnn (one to three digits)
/xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
/cx a control-x character
$加双引号($""):实际上与locale设置有关,在C和POSIX下的locale下等同于没带$的双引号
原文:cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored.
第二层:echo接受参数,处理,显示结果
不加-e:按字符显示
加-e: 判断并显示转义(就算你原本打算传进去的就是二个字符,比如'/t')
先看以下例子:
sosodream@ubuntu:~>echo '/t'|xxd + xxd + echo '/t' 0000000: 5c74 0a /t. sosodream@ubuntu:~>echo -e '/t'|xxd + xxd + echo -e '/t' 0000000: 090a .. sosodream@ubuntu:~>echo $'/t'|xxd + xxd + echo ' ' 0000000: 090a .. sosodream@ubuntu:~>echo -e $'/t'|xxd + xxd + echo -e ' ' 0000000: 090a .. sosodream@ubuntu:~>echo "/t"|xxd + xxd + echo '/t' 0000000: 5c74 0a /t. sosodream@ubuntu:~>echo -e "/t"|xxd + xxd + echo -e '/t' 0000000: 090a .. sosodream@ubuntu:~>echo $"/t"|xxd + xxd + echo '/t' 0000000: 5c74 0a /t. sosodream@ubuntu:~>echo -e $"/t"|xxd + xxd + echo -e '/t' 0000000: 090a .. sosodream@ubuntu:~>
实际上,分析单双引号,要从二个层面上去考虑以上边的例子:
第一层:shell传递参数给echo前进行的扩展解析
第二层:echo接受参数,处理,显示结果
具体分析如下:
第一层:shell传递参数给echo前进行的扩展解析
单引号:shell单引号不进行字符转义
原文:Enclosing characters in single quotes preserves the literal value of each character within the quotes.
双引号:不进行字符转义,除了三个字符$, `, /,和开启命令扩展后的!字符
$加单引号(即$''):除了以下字符转义,其他同单引号
/a alert (bell)
/b backspace
/e an escape character
/f form feed
/n new line
/r carriage return
/t horizontal tab
/v vertical tab
// backslash
/' single quote
/nnn the eight-bit character whose value is the octal value nnn (one to three digits)
/xHH the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
/cx a control-x character
$加双引号($""):实际上与locale设置有关,在C和POSIX下的locale下等同于没带$的双引号
原文:cause the string to be translated according to the current locale. If the current locale is C or POSIX, the dollar sign is ignored.
第二层:echo接受参数,处理,显示结果
不加-e:按字符显示
加-e: 判断并显示转义(就算你原本打算传进去的就是二个字符,比如'/t')
相关文章推荐
- shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符`
- 转载:shell单引号字符' 双引号字符" 反斜杠字符/ 反引号字符
- shell中的单引号字符' 双引号字符" 反斜杠字符/ 反引号字符`
- Shell十三问之四:""(双引号)与' '(单引号)差在哪
- Oracle 内的转义字符 单引号'和&符的问题
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符`
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- Linux中单引号(''),双引号("")与反单引号(``)的用法与区别
- xml 转义特殊字符 如&'"
- php 输出双引号"与单引号''的方法
- xml 转义特殊字符 如&'"
- shell中" "(双引号)与 ' '(单引号)的区别
- [求教] - 关于ASP中如何对数据表字段中所包含的特殊字符 ' 与 " 进行转义处理?
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- 关于js中单引号(')和双引号(")的使用以及转义的理解
- PHP中双引号""与单引号''使用区别
- 关于js中单引号(')和双引号(")的使用以及转义的理解