How to grep for the dollar symbol ($)?

后端 未结 6 375
轮回少年
轮回少年 2020-12-04 18:37
% cat temp
$$$ hello1
$$  hello2
    hello3
##  hello4
    hello5 $$$
% cat temp | grep \"$$$\"
Illegal variable name.
% cat temp | grep \"\\$\\$\\$\"
Variable name          


        
相关标签:
6条回答
  • 2020-12-04 19:15

    Same issue with this command

    complete -W "\`grep -oE '^[a-zA-Z0-9_.-]+:([^=]|$)' Makefile \`"
    

    Illegal variable name.

    0 讨论(0)
  • 2020-12-04 19:24

    How about ^.*[$]{3}.*$

    0 讨论(0)
  • 2020-12-04 19:25

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

    0 讨论(0)
  • 2020-12-04 19:30

    Works for me:

    user@host:~$ cat temp | grep '\$\$\$'
    $$$ hello1
    hello5 $$$
    user@host:~$ 
    
    0 讨论(0)
  • 2020-12-04 19:31
    $ grep '\$\$\$' temp
    $$$ hello1
    hello5 $$$
    

    There's a superflous 'cat' in your command.

    0 讨论(0)
  • 2020-12-04 19:38

    When you use double quotes " or none use double\: "\\\$\\\$\\\$"

    cat t | grep \\\$\\\$\\\$ 
    

    if you use in single quotes ' you may use:

    cat t | grep '\$\$\$'
    
    0 讨论(0)
提交回复
热议问题