parimal
parimal

Reputation: 19

Unexpected behavior of 'grep'

Can someone please explain the behavior of grep for the below case:

$ grep <html> foo
$ bash: html: No such file or directory

Upvotes: 1

Views: 95

Answers (2)

estani
estani

Reputation: 26447

$ grep <html> foo

is

$ cat html | grep > foo

which reads the file html sends it to grep and stores the result in a file called foo. See hurb's answer for an extended explanation

Upvotes: 0

hurb
hurb

Reputation: 2217

You need to escape < and > inside bash.

  • Use a backslash to escape a single character
  • Use single quotes to escape multiple characters

Correct syntax:

grep '<html>' myfile
grep \<html\> myfile

Further information:

< and > are used for I/O Redirection. < accepts input and > redirects your output. Thus, grep <html> foo tries to read the file named html and redirects the output into the file myfile.

Upvotes: 1

Related Questions