shell variable in a grep regex

前端 未结 1 1311
一生所求
一生所求 2020-12-13 08:53

I\'m trying to use a variable in a grep regex. I\'ll just post an example of the failure and maybe someone can suggest how to make the variable be evaluated while running t

相关标签:
1条回答
  • 2020-12-13 09:17

    You need to use double quotes. Single quotes prevent the shell variable from being interpolated by the shell. You use single quotes to prevent the shell from doing interpolation which you may have to do if your regular expression used $ as part of the pattern. You can also use a backslash to quote a $ if you're using double quotes.

    Also, you may need to put your variable in curly braces ${var} in order to help separate it from the rest of the pattern.

    Example:

    $ string="test this"
    $ var="test"
    $ echo $string | grep "^$var"
    
    0 讨论(0)
提交回复
热议问题