grep'\ $'和grep"\ $"之间有什么区别? [英] What's the differences between grep '\$' and 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 allowsbash
escapes, just like above; the command being executed is again$
.In
grep '\$' a_file
,bash
interprets single quotes, which basically tellbash
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屋!