From the manual for grep, the general form of grep usage is:

grep options pattern input_file_names

In particular, it is stated "There can be zero or more input file names."

What files are candidates for the search, if there are zero file names? Ex. grep mystr


From man grep:

   file   A pathname of a file to be searched for the patterns. If no file
          operands are specified, the standard input shall be used.

So it will wait for you type some text if there's no pipeline or redirection involved.

  • Thanks for the link. I was reading gnu.org/software/grep/manual/grep.pdf and it doesn't have the info you quoted. Are these manuals written by different people? I assume there is only one grep? – boxofchalk1 Nov 18 '14 at 21:21
  • 1
    There many greps. I linked to the POSIX manual, which most forms of grep will treat as a base, including the GNU grep. The manual you linked to does mention this, but in a more verbose way (see section 2.4). – muru Nov 18 '14 at 21:29
  • 1
    @boxofchalk1 Actually, if you'd said you are using GNU grep, I would have linked to a manpage for the GNU grep: manpages.ubuntu.com/manpages/trusty/en/man1/grep.1.html, which has this information in the first line. – muru Nov 18 '14 at 21:35
  • Thanks muru. Actually, I did not know there were different greps. But I just did grep -V and found I am indeed running GNU grep (which came with CentOS). This is off-topic, but are there Linux distributions that installs the POSIX version instead? – boxofchalk1 Nov 19 '14 at 2:34
  • 1
    @boxofchalk1 there is no single POSIX version, since it is a standard that's implemented by others. On Linux, you are certain to be using GNU grep, since Linux and GNU go hand in hand (that's why it's sometimes called GNU/Linux). Debian has the Plan9 grep in its repositories, but i don't know if CentOS has that. – muru Nov 19 '14 at 8:15

It reads from its standard input (file descriptor 0) if not given any filename.

grep foo

is equivalent to:

grep foo -

That allows things like:

grep foo < file


cmd | grep foo

If grep foo is run at the prompt of an interactive shell in a terminal, then grep will read from the terminal device, so from what you enter via the keyboard.

  • Thanks for the detailed explanation! As muru's answer came first and resolved my question, I've accepted that answer. Thanks again. – boxofchalk1 Nov 18 '14 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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