Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to use grep with a PCRE expression that contains the < character. Bash thinks I want to redirect, but I don't want to. How can I escape <?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

I was able to do it with a backslash:

25 % grep \< xmospos.c
#include        <stdio.h>
#include        <stdlib.h>
#include        <getopt.h>
#include        <X11/Xlib.h>

A quoted less-than, and a quoted, backslashed less than both gave goofy answers.

share|improve this answer
Yes, or put it in single quotes –  glenn jackman Jul 26 '11 at 1:51
I still need to learn, e.g. how that exactly works with escaping: grep -oP '(?< )(.*)$' does not work with single quotes for example. Tried it at first. –  hakre Jul 26 '11 at 8:50
@hakre There seems to be a syntax error in your regexp. Did you mean (?<! )(.*)$ or (?<= )(.*)$? –  Gilles Jul 26 '11 at 22:18

Two simple rules:

  • A backslash \C escapes the next character, whatever it is, other than a newline.
  • Single quotes 'text' escape any character between them, including a backslash, but not including a single quote (since it marks the end of the quoted text).


grep -P '<html>' myfile
grep -P \<html\> myfile
grep -P '(?<!<)html' myfile
grep -P \(\?\<\!\<\) myfile

If you need to pass an argument that contains single quotes, you can use '\'' to “escape” a single quote inside single quotes. Technically, what this does is end the first literal text, then put a literal ' in the same word, then more literal text still in the same word.

grep '^D'\''oh!' myfile

The rest of the quoting rules (summarized):

  • You need to quote the following characters at least some of the times: whitespace and !"#$&'()*;<>?[\]^`{|}~ (in other words, the following characters are safe: letters, digits, %+,-./:=@_ and non-ASCII characters).
  • Between double quotes, all characters are used literally except "$\` and (in shells with history enabled) !. A backslash between double quotes will appear in the string unless it precedes one of the non-literal characters.

These rules are for bash and other Bourne-style shells (such as ash and ksh). They apply to zsh as well (except that = after whitespace may need quoting). The rules in csh/tcsh and in Fish are different.

share|improve this answer
Thanks a lot for the detailed explanation. –  hakre Jul 27 '11 at 4:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.