- 论坛徽章:
- 0
|
楼主所说“为什么tr的参数不加单引号就会出现这种情况呢?”,根据上述情况,楼主都加了单引号的。
关键是touch n这个命令,该问题确实比较奇怪,后来反复试验一番,结果如下:
一、没有使用touch n命令前:
[xfm@debian:~]$ echo 'hello, world!' | tr -d [:punct:]
hello world
以上运行结果正常。
二、使用set -x来看下命令的执行过程:
[xfm@debian:~]$ set -x
[xfm@debian:~]$ echo 'hello, world!' | tr -d [:punct:]
+ tr -d '[:punct:]'
+ echo 'hello, world!'
hello world
以上运行结果正常。
三、使用touch n命令后:
[xfm@debian:~]$ echo 'hello, world!' | tr -d [:punct:]
+ tr -d n
+ echo 'hello, world!'
hello, world!
以上运行结果不正常。
二和三运行的结果不一样,相信各位一看就明白了。
tr -d '[:punct:]'删除'hello, world!'中的标点符号,所以运行结果为hello world
tr -d n只删除"n"这个字母,由于
'hello, world!'中没有该字符,所以运行结果为hello, world!
PS:
如果把touch n中的"n"改为其它任何字符(或字符串),其运行结果都是正确的--hello world
如果把echo 'hello, world!' | tr -d [:punct:]中的[:punct:]改为[[:punct:]],即使运行touch n命令后其运行的结果也是正确的--hello world
[xfm@debian:~]$ echo 'hello, world!' | tr -d [[:punct:]]
+ tr -d '[[:punct:]]'
+ echo 'hello, world!'
hello world
不知大家如何理解? |
|