Re: Escaping grep

bodiccea <[email protected]>
Newsgroups alt.comp.lang.shell.unix.bourne-bash
Organization Guest of ProXad - France
Message-ID <20210417175837.3c83dcab@lorien>
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

bodi.
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.