Overview

General

grep PATTERN PATH

From file

cat PATH | grep PATTERN

From string

echo 'TEXT' | grep PATTERN

In a directory such as ., * or a directory name.

grep PATTERN DIR

grep -r PATTERN DIR

Use -i or -ir for case insensitive.

File

$ grep PATTERN foo.txt
$ grep PATTERN *.txt

Directory

$ grep -r PATTERN .
$ grep -r PATTERN my_dir

stdin

If you omit the paths, then grep reads from stdin.

echo 'hello world' | grep 'hello'

Heredoc

Use a heredoc or herestring to pass a multiline string.

See Here strings on Wikipeida.

Inline

$ grep PATTERN <<< STRING

e.g.

One line.

$ grep 'hello' <<< 'hello world'
hello world

Multiple lines.

$ grep 'hello' <<< 'hello world
and hello devs
greetings'
hello world
and hello devs

Using multi-line variable

MY_VAR='hello world'

grep 'hello' <<< $MY_VAR

Maybe quotes is safer on the variable, but might not make a difference for heredocs.