如何grep美元符号($)? [英] How to grep for the dollar symbol ($)?

查看:485
本文介绍了如何grep美元符号($)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

% cat temp
$$$ hello1
$$  hello2
    hello3
##  hello4
    hello5 $$$
% cat temp | grep "$$$"
Illegal variable name.
% cat temp | grep "\$\$\$"
Variable name must contain alphanumeric characters.
%

我想为 $$$ ,我预计结果是

I want to grep for $$$ and I expect the result to be

% cat temp | grep <what should go here?>
$$$ hello1
    hello5 $$$
%

为了区分,我标记了提示符为

To differentiate, I have marked the prompt as %.


  • 什么是问题这里<?li>
  • grep string 应该是什么?

  • What is the problem here?
  • What should the grep string be?

推荐答案

问题在于shell在双引号字符串内展开变量名称。因此,对于$$$,它会尝试从第一个 $ 开始读取变量名称。

The problem is that the shell expands variable names inside double-quoted strings. So for "$$$" it tries to read a variable name starting with the first $.

另一方面,在单引号中,变量不会被扩展。因此,'$$$' 工作 - 如果不是因为 $ 是表示行尾的正则表达式中的特殊字符。所以它需要被转义:'\ $ \ $ \ $'

In single quotes, on the other hand, variables are not expanded. Therefore, '$$$' would work – if it were not for the fact that $ is a special character in regular expressions denoting the line ending. So it needs to be escaped: '\$\$\$'.

这篇关于如何grep美元符号($)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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