Re: Escaping grep

"Michael F. Stemper" <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.