Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The << operator, known as a Here-Document (or heredoc), is an I/O redirection mechanism in Bash. It instructs the shell to read a block of string literals from the current input stream and redirect it to the standard input (stdin) of a specified command. The shell continues reading until it encounters a predefined, user-specified delimiter token on a line by itself.
command << DELIMITER
String content line 1
String content line 2
DELIMITER

Parsing and Expansion Mechanics

By default, the shell parses the body of a Here-Document similarly to a double-quoted string. It subjects the content to three specific types of expansion before passing it to the command:
  1. Parameter Expansion ($VAR)
  2. Command Substitution ($(command) or `command`)
  3. Arithmetic Expansion ($((expression)))
To prevent the shell from evaluating a specific character (like $, `, or \), it must be escaped using a backslash (\).

Delimiter Quoting (Literal Mode)

If any character of the opening delimiter is quoted (using single quotes, double quotes, or a backslash), the shell alters its parsing behavior. It disables all expansions within the Here-Document body, treating the entire block as a raw, literal string.

# Single quotes
command << 'EOF'
$VAR will not be expanded here.
EOF


# Double quotes
command << "EOF"
$(command) will not execute here.
EOF


# Backslash escape
command << \EOF
$((1 + 1)) remains literal text.
EOF

The <<- Variant (Tab Stripping)

Appending a hyphen to the operator (<<-) modifies the shell’s whitespace handling. When <<- is used, the shell strips all leading tab characters (\t) from every line within the Here-Document body, as well as from the line containing the closing delimiter. This variant exists strictly for source code formatting, allowing the Here-Document to be indented to match the surrounding script structure without passing the indentation to the command’s stdin.
if true; then
	command <<- EOF
		This line has leading tabs stripped.
		So does this one.
	EOF
fi
Note: The <<- operator strictly strips tab characters. Leading spaces are preserved and passed to the command.

Termination Constraints

The shell enforces strict lexical rules for the closing delimiter:
  • It must be the exact character sequence specified by the opening delimiter.
  • It must reside on its own line.
  • It cannot be preceded by any characters (except tabs if <<- is used).
  • It cannot be followed by any characters, including trailing spaces, semicolons, or comments.
Master Bash with Deep Grasping Methodology!Learn More