grep'\ $'和grep"\ $"之间有什么区别? [英] What's the differences between grep '\$' and grep "\$"?

查看:83
本文介绍了grep'\ $'和grep"\ $"之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grep'\ $'和grep"\ $"有什么区别?

有人可以对下面的命令给出正确的解释.

 <代码>/usr/download/test/myshell $ cat a_file开机书豪饮机器靴子邦吉吠土豚碎的凝灰岩机器人 

输出:grep'\ $'a_file

 <代码>/usr/download/test/myshell $ grep'\ $'a_file碎的凝灰岩 

输出:grep"\ $" a_file

 /usr/download/test/myshell $ grep"\ $" a_file开机书豪饮机器靴子邦吉吠土豚碎的凝灰岩机器人 

输出:grep \ $ a_file

 <代码>/usr/download/test/myshell $ grep \ $ a_file开机书豪饮机器靴子邦吉吠土豚碎的凝灰岩机器人 

解决方案

\ bash grep 的转义字符./p>

  • grep \ $ a_file 中, bash 认为您正在转义 $ ,但是不需要转义,因此正在执行的regexp是 $ ,它会查找任何有结尾的行(毫无疑问,所有行都有).

  • grep"\ $" a_file 中, bash 解释双引号,从而允许在其中使用各种卑鄙的内容.特别是,它仍然允许 bash 转义,就像上面一样;再次执行的命令是 $ .

  • grep'\ $'a_file 中, bash 解释单引号,这基本上告诉 bash 将内容完整保留为尽可能地.值得注意的是, bash 不会删除反斜杠.正则表达式将为 \ $ ,其中 \ 从美元中删除行尾"的特殊含义,并且正在搜索文字美元字符.

有关更多详细信息,请参见 QUOTING 下的 man bash .

What's the differences between grep '\$' and grep "\$"?

Could someone give a proper explanation for the below command.

/usr/download/test/myshell$ cat a_file
boot
book
booze
machine
boots
bungie
bark
aardvark
broken$tuff
robots

Output: grep '\$' a_file

/usr/download/test/myshell$ grep '\$' a_file 
broken$tuff

Output: grep "\$" a_file

/usr/download/test/myshell$ grep "\$" a_file 
boot
book
booze
machine
boots
bungie
bark
aardvark
broken$tuff
robots

Output: grep \$ a_file

/usr/download/test/myshell$ grep \$ a_file 
boot
book
booze
machine
boots
bungie
bark
aardvark
broken$tuff
robots

解决方案

\ is an escape character for both bash and grep.

  • In grep \$ a_file, bash thinks you are escaping $, but it doesn't need escaping, so the regexp being executed is $, which looks for any line that has an end (unsurprisingly, all of them do).

  • In grep "\$" a_file, bash interprets double quotes, which allow a variety of bashy things to go on inside. Specifically, it still allows bash escapes, just like above; the command being executed is again $.

  • In grep '\$' a_file, bash interprets single quotes, which basically tell bash to keep things inside intact as much as possible. Notably, bash will not remove the backslash. The regexp will be \$, where \ removes the special meaning of "end of line" from the dollar, and a literal dollar character is being searched for.

See man bash under QUOTING for more details.

这篇关于grep'\ $'和grep"\ $"之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆