linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Simulator
  • Knowledge base
  • Interview prep
Index
Categories
All entries
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
home/linux/kb/Processes & resources/heredoc

kb/processes ── Processes & resources ── beginner

Here-doc and here-string: data inside the script

Here-doc (`<<EOF ... EOF`) feeds multi-line text to a command's stdin with no temp file. Here-string (`<<<`) does the same for a single line.

view as markdownaka: here-document, here-string, heredoc-eof

Here-doc: multi-line stdin

When you need to feed a command multi-line text without creating a temp file:

bash
cat <<EOF > /etc/myapp/config.ini
[server]
host = localhost
port = 8080
user = ${USER}
EOF

Everything between <<EOF and the closing EOF goes to the command's stdin (here that is cat, which writes to a file through >). EOF is just a marker; you can use any word (END, DONE, __SCRIPT__).

Variable expansion: with quotes or without

By default ${VAR} and $(...) expand inside a here-doc:

bash
cat <<EOF
Hello, ${USER}!
Today is $(date +%F)
EOF

▸Hello, student!

▸Today is 2026-04-29

If you want literal text with no expansion, quote the marker:

bash
cat <<'EOF' > script.template
Hello, ${USER}!     # ← does NOT expand, stays as ${USER}
$HOME too           # ← does NOT expand
EOF

This matters when you generate scripts, templates, or Dockerfile fragments where $VAR must stay as literal text.

Stripping leading tabs: <<-

Inside indented blocks (if, function) a here-doc looks ugly, because the indentation ends up in the text. The fix is <<- (minus): bash strips leading tabs (not spaces):

bash
if [[ -d /etc ]]; then
    cat <<-EOF
		this message
		has leading tabs
		that bash will strip
	EOF
fi

Tabs only. If your editor inserts spaces, it will not work.

Here-string: <<< for a single line

The single-line counterpart:

bash
grep apple <<< "i like apple pie"

▸grep reads "i like apple pie" from stdin as if from a file

▸same as: echo "i like..." | grep apple, but with NO pipe and no echo subprocess

Handy for bc, jq, any command that wants stdin data:

bash
bc <<< "2 + 2"           # 4
jq '.name' <<< '{"name":"alice"}'   # "alice"

Common mistakes

  • A marker with trailing spaces: bash will not close the here-doc:
    bash
    cat <<EOF
    text
    EOF       <-- space after EOF! will not close
  • <<- with spaces instead of tabs: the stripping will not happen.
  • Cross-platform: <<< exists in bash and zsh, not in /bin/sh. For POSIX compatibility, use printf '...' | cmd.

§ команды

bash
cat <<'EOF' > file.txt\nbody...\nEOF

Write multi-line text to a file with no variable expansion

bash
ssh host bash <<EOF\ncmd1\ncmd2\nEOF

Run many commands on a remote host over one connection

bash
grep pattern <<< "$var"

Pass the contents of a variable to grep as stdin (no echo + pipe)

bash
python3 <<EOF\nimport sys\nprint(sys.version)\nEOF

Embedded script: run inline Python from bash

§ см. также

  • file-descriptorsFile descriptors (stdin, stdout, stderr)A file descriptor is an integer a process uses to reach an open file, socket, or pipe. Every process gets 0/1/2 = stdin/stdout/stderr.
  • process-and-pidProcess and PIDA process is a running program with its own PID, memory, open descriptors, and UID. Every process forms a tree rooted at init (PID 1).
  • bash-strict-modeBash strict mode: set -euo pipefailThree flags at the top of a bash script that turn it from forgiving into fail-on-the-first-error. Without them, bugs pile up silently.

§ упоминается в уроках

  • ›beginner-12-shell-scripting
  • ›intermediate-11-advanced-redirects
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies