Re: Escaping grep

"Michael F. Stemper" <[email protected]> Sat, 17 Apr 2021 11:08:36 -0500
Newsgroups alt.comp.lang.shell.unix.bourne-bash
Organization A noiseless patient Spider
Message-ID <[email protected]>
On 17/04/2021 10.58, bodiccea wrote:
> On Sat, 17 Apr 2021 15:36:19 +0200, bodiccea <[email protected]> wrote:
>> You have 2 solutions:
>> 1) use quotes instead of double quotes: '^\\usep', so that bash does
>> not change anything.
>> 2) using double quotes, force bash to preserve 2 backslash with the
>> string "^\\\\usep"
> 
> As a general rule, when you are unsure about "real" strings sent to a
> program (like grep in your case), you can always test them with printf,
> I do it sometimes with complicated patterns.
> 
> Examples:
> # your initial string
> $ printf "%s\n" "^\\usep"
> ^\usep                           <-- passed to grep, incorrect
> 
> # with quotes
> br@lorien:~/.claws-mail$ printf "%s\n" "^\\\\usep"
> ^\\usep                          <-- passed to grep, correct
> 
> # with double quotes
> br@lorien:~/.claws-mail$ printf "%s\n" '^\\usep'
> ^\\usep                          <-- passed to grep, correct

That's a really good idea. Thanks for the pro-tip. (But I think
that your last comment has a typo.)


-- 
Michael F. Stemper
Outside of a dog, a book is man's best friend.
Inside of a dog, it's too dark to read.