Frequently Asked Question

What is a here-string (<<<) and how is it different from a here-doc?

A here-string feeds a single line of text to a command's standard input, written with three less-than signs followed by the string: grep foo <<< "this foo line". It is a bash extension, POSIX shells do not have it, but it is supported by bash, zsh, and ksh. The shell evaluates the string (expanding variables and command substitutions unless quoted), then connects it as stdin to the command.

Here-strings are most useful for commands that only read from stdin when you have a value already in a variable. Instead of echo "$var" | grep pattern, which forks a subprocess for echo, you can write grep pattern <<< "$var", which is a tiny bit faster and arguably clearer. They also avoid the trailing newline subtleties that pipes from echo can introduce. For more than one line of input, reach for a here-doc instead.

Further reading and video