Bash Here Documents
A here document feeds several lines of text into a command. The text sits right inside the script, so no separate file is needed. Scripts use it for long messages, configuration files, and menus.
The Idea in a Picture
command << MARKER
line one
line two
line three
MARKER
|
v
The command receives the three lines as input
The marker word starts the block and ends it. Any word works. Writers pick EOF (end of file) by habit.
Basic Example
#!/bin/bash
cat << EOF
Welcome to the Backup Tool
Version 1.0
Press Ctrl+C to stop
EOFOutput:
Welcome to the Backup Tool
Version 1.0
Press Ctrl+C to stopThe closing marker must stand alone at the start of its line, with no spaces before or after it.
Variables Inside the Block
Bash replaces variables and commands inside a here document:
#!/bin/bash
name="Asha"
cat << EOF
Hello, $name
Today is $(date +%A)
Your folder is $(pwd)
EOFOutput:
Hello, Asha
Today is Friday
Your folder is /home/ashaStop Expansion With Quotes
Quotation marks around the marker switch replacement off. The text prints exactly as typed.
cat << 'EOF'
The variable $name stays as text
The command $(date) stays as text
EOFIndent With a Dash
The form <<- removes leading tab characters. Code inside functions and loops stays neatly aligned.
show_help() {
cat <<- EOF
Usage: tool [option]
-h show help
-v show version
EOF
}This form strips tabs only. Spaces remain in the output.
Write a File
cat > settings.conf << EOF
host=localhost
port=8080
mode=testing
EOFThe redirect > saves the block into a new file. Use >> to append to an existing file instead.
Write a File With Administrator Rights
sudo tee /etc/myapp.conf << EOF > /dev/null
mode=production
EOFA plain redirect fails here because the redirect runs without administrator rights. The tee command receives the rights from sudo.
Send Input to Another Command
sort << EOF
mango
apple
banana
EOFOutput:
apple
banana
mangoRun Multi-Line Commands
bash << 'EOF'
for i in 1 2 3
do
echo "Run $i"
done
EOFBash reads the block as a small script and runs it.
Here Strings
A here string passes one short piece of text with three angle brackets:
grep "apple" <<< "I like apple pie"
wc -w <<< "count these four words"The second line prints 4.
Practical Example: Help Message
#!/bin/bash
usage() {
cat << EOF
Usage: $0 [options] file
Options:
-h Show this help message
-v Show detailed output
-o Set the output folder
EOF
}
if [ $# -eq 0 ]; then
usage
exit 1
fiThe function prints a tidy help screen when a user runs the script without arguments.
Common Mistakes
- Spaces before the closing marker keep the block open and trigger a "here-document delimited by end-of-file" warning.
- Different spelling in the opening and closing markers has the same effect.
- Dollar signs in text need a quoted marker or a backslash to stay as typed.
Key Takeaways
<< MARKERsends a block of text to a command.- Variables and commands expand unless the marker sits in quotes.
<<-removes leading tabs for tidy code.- Combine with
>to create files from a script. <<<sends a single string as input.
