% 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 %
.
grep is very often used as a "filter" with other commands. It allows you to filter out useless information from the output of commands. To use grep as a filter, you must pipe the output of the command through grep . The symbol for pipe is " | ".
grep is a program that searches for regular expressions. The first argument for grep is the pattern to look for. In scripts and functions $1 is a reference to the first argument passed to that script or function.
To match a character that is special to grep –E, put a backslash ( \ ) in front of the character. It is usually simpler to use grep –F when you don't need special pattern matching.
The answer is to use either \$ or single quotes. echo '$PATH' will print $PATH echo '\$PATH' will print \$PATH echo "\$PATH" will print $PATH echo ''\$PATH'' will technically do the right thing, but the two null strings serve no purpose.
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: '\$\$\$'
.
When you use double quotes "
or none use double\: "\\\$\\\$\\\$"
cat t | grep \\\$\\\$\\\$
if you use in single quotes ' you may use:
cat t | grep '\$\$\$'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With