Search code examples
regexkshquote

Escapes quotes fine on command line but doesn't work when assigned to variable


Very simple problem but I can't see what the issue is. In ksh shell escaping a quote in searchstring works differently when assigned to a variable versus when used from the command line. All the examples I can find demonstrate using the command line so couldn't find a match for what is a very basic problem.

Both work fine:

cat access_443_log.2020-01-09 | grep 'HTTP/1.1" 200 '

cat access_443_log.2020-01-09 | grep "HTTP/1.1\" 200 "

But doing it when either is assigned to the variable searchstring

searchstring='HTTP/1.1" 200 ' or searchstring="HTTP/1.1\" 200 "

cat access_443_log.2020-01-09 | grep $searchstring

Error: grep: 0652-033 Cannot open 200.

Thanks.


Solution

  • Try to add double quote to your variable, like this.

    cat access_443_log.2020-01-09 | grep "$searchstring"