Linux Command Reference ยท Bash Syntax
Bash here-documents and here-strings
Reference for Bash here-documents and here-strings: feed multi-line text to a command, control expansion with quoting, strip tabs with <<- and use <<< for one-liners.
A here-document (<<) lets you write several lines of text directly inside a script and give them to a command as its standard input. The shell reads lines until it meets a line that contains only the delimiter word you chose. It is the usual way to embed a block of text, a config file or an SQL script in a shell script without a pile of echo calls.
A here-string (<<<) is the one-line cousin: it sends a single word or string, followed by a newline, to the standard input of a command. It replaces the common echo "$x" | command pattern without starting an extra process.
command <<DELIMITER text lines DELIMITER command <<< "one string"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
[n]<<[-]WORD here-document DELIMITER [n]<<< word
| Part | Description |
|---|---|
<<WORD |
Read the following lines up to a line consisting only of WORD. Expansions (variables, command substitution, arithmetic, backslash escapes for $, ` and \) happen inside. |
<<'WORD', <<"WORD", <<\WORD |
Quoting any part of the word turns expansion off; the body is taken literally. |
<<-WORD |
Like <<, but leading tab characters are removed from every body line and from the delimiter line, so the block can be indented. |
<<< word |
Here-string: word is expanded (parameters, command substitution, arithmetic, quote removal, but no word splitting or globbing) and sent with a trailing newline. |
n |
Optional file descriptor number (default 0, standard input). |
The delimiter line must contain the word and nothing else: no trailing spaces, no leading spaces (unless <<- with tabs). Otherwise the shell keeps reading until end of file and prints a warning.
Usage notes
- Quote the delimiter (
<<'EOF') whenever the text contains$, backticks or backslashes that must stay as they are, for example when you generate another shell script or a config file. - With an unquoted delimiter you still need
\$to print a literal dollar sign. Quotes inside the body are never special; they are printed as typed. <<-removes tabs only. An editor that converts tabs to spaces silently breaks the block, and the delimiter is then not recognised.- The here-document is a redirection, so it can sit after the command, after other redirections, or at the end of a pipeline stage:
cat <<EOF | sort. Several here-documents on one line are read in order. - To pass the body to a command inside a pipeline or
$(...), remember that the body starts on the line after the command, even if the command is not finished:x=$(cat <<EOFbegins the body on the next line. - Here-strings add a trailing newline.
wc -c <<< "abc"prints 4, not 3. Useprintfwith a pipe if you need exact bytes. - A here-string does not split on spaces by itself;
readdoes that according toIFS. Because no pipe is involved,readruns in the current shell, so the variables survive:echo hi | read xlosesxin most shells, whileread x <<< hikeeps it. - Both are Bash (and ksh/zsh) features; a here-string is not available in a plain POSIX
shsuch as dash. Here-documents are POSIX. - Large bodies are stored in a temporary file or pipe by the shell; this is fine for scripts, but for huge data prefer a real file.
Related
- Pipelines
- Command lists
- cat command
- Tutorial: Here-documents