0

In Advanced Bash-Scripting Guide, I find

Within single quotes, every special character except ' gets interpreted literally.

So I think grep '\<the\>' file.txt would search \<the\>, instead of word the. But it searches the indeed.

#!/bin/bash

grep '\<the\>' file.txt

Added
Maybe I don't describe my question clearly.In man page,

Enclosing characters in single quotes preserves the literal value of each character within the quotes.

So my question is: Now that bash would regard enclosing characters in single quote as the literal value, why '\<the\>' is treated as the in grep? Is it grep own characteristic,differing from bash?

Hel
  • 305
  • 5
  • 14

2 Answers2

3

Indeed, bash will pass your string literally.

It is grep that interpretes the string (as a regular expression). If you want to avoid that, use grep -F. With that option, grep will search literally for the given string.

Alex O
  • 7,746
  • 2
  • 25
  • 38
2

You need to add another backslash \ to match the whole pattern, as the symbols \< and \> are special to grep. Quoting the manpage: man grep

The Backslash Character and Special Expressions

  The symbols \< and \> respectively match the empty string at the beginning and end
  of  a  word. 

enter image description here

xxfelixxx
  • 6,512
  • 3
  • 31
  • 38
  • 1
    Indeed, `grep` is a poor example for understanding quoting, because many of the characters in regular expressions have a special meaning to `grep`. – tripleee Aug 23 '16 at 03:57