Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

% 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 %.

  • What is the problem here?
  • What should the grep string be?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
783 views
Welcome To Ask or Share your Answers For Others

1 Answer

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: '$$$'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...