% cat temp
$$$ hello1
$$ hello2
hello3
## hello4
hello5 $$$
% cat temp | grep "$$$"
Illegal variable name.
% cat temp | grep "\$\$\$"
Variable name must contain alphanumeric characters.
%
我想为 $$$
grep 并且我希望结果是
% cat temp | grep <what should go here?>
$$$ hello1
hello5 $$$
%
为了区分,我将提示标记为%
。
- 这里有什么问题?
- grep 字符串应该是什么?
最佳答案
问题在于 shell 扩展了双引号字符串中的变量名。因此,对于 "$$$"
,它会尝试读取以第一个 $
开头的变量名。
另一方面,在单引号中,变量不会被扩展。因此,'$$$'
会起作用——如果不是因为 $
是正则表达式中表示行的特殊字符结尾。所以需要转义:'\$\$\$'
.
关于linux - 如何grep美元符号($)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3759926/