Hel
Hel

Reputation: 305

Do bash script and command grep treat single quote differently

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?

Upvotes: 0

Views: 111

Answers (2)

Alex O
Alex O

Reputation: 8164

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.

Upvotes: 3

xxfelixxx
xxfelixxx

Reputation: 6592

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

Upvotes: 2

Related Questions