Here Documents
Here documents (heredocs) are a convenient way to handle multi-line text blocks in Bash scripts. They're perfect for generating configuration files, creating SQL queries, writing multi-line messages, and passing large text blocks to commands. Here strings are a related feature for single-line text input. Both features make your scripts cleaner and more readable!
Basic Here Document Syntax
A here document starts with << followed by a delimiter (commonly EOF for "End Of File"). Everything until the delimiter is treated as input to the command.
Click Run to execute your code
# Basic heredoc syntax
command <
- The delimiter can be any word (EOF, END, STOP, etc.)
- The ending delimiter must be on a line by itself, with no leading/trailing spaces
- Variables and command substitution are expanded by default
- Heredocs work with any command that reads from stdin
Variable Expansion in Heredocs
By default, variables and command substitutions are expanded in here documents, making them perfect for templates!
name="Alice"
age=25
cat <
Quoted Delimiters (No Expansion)
If you quote the delimiter (with single or double quotes, or a backslash), variable expansion is disabled. This is useful when you want literal text.
name="Alice"
# With expansion (default)
cat <
<<'EOF' or <<\EOF) when generating scripts, config files with literal dollar signs, or when you want to preserve special characters exactly as written.
Here Strings: <<<
Here strings are a shorthand way to pass a single string to a command's stdin. They're useful for quick one-liners.
# Here string syntax: <<< "text"
# Convert to uppercase
tr 'a-z' 'A-Z' <<< "hello world"
# Output: HELLO WORLD
# Pass to grep
grep "error" <<< "This line has an error message"
# Pass variable to command
text="Hello World"
wc -w <<< "$text"
# Output: 2
- Quick text processing with commands like
tr, grep, sed- Testing commands with sample input
- Passing variables to commands that expect stdin
- One-liners without creating files
Practical Examples
Here documents shine in real-world scenarios. Here are some common use cases:
# 1. Generate configuration file
cat > /tmp/config.txt < deploy.sh <<'EOF'
#!/bin/bash
# Auto-generated deployment script
echo "Deploying application..."
# More commands here
EOF
chmod +x deploy.sh
Common Mistakes
1. Spaces around delimiter
# Wrong - spaces break the delimiter
cat <
2. Using wrong delimiter
# Wrong - delimiter mismatch
cat <
3. Forgetting to quote when needed
# Wrong - unwanted expansion when generating script
cat > script.sh < script.sh <<'EOF'
#!/bin/bash
echo "Price: $10" # This stays as $10 (literal)
EOF
Exercise: Generate a Configuration File
Task: Use a here document to generate a configuration file!
Requirements:
- Create a config file with at least 3 settings
- Use variables for at least one setting
- Include a comment in the config file
- Use a here string to count words in a message
Hint: Remember to use quoted delimiter if you want literal dollar signs in your config!
Show Solution
#!/bin/bash
# Generate configuration file
# Variables for config
APP_NAME="MyApp"
PORT=8080
# Create config file with heredoc
cat > app.conf <
Summary
- Here Document:
<<DELIMITER...DELIMITERfor multi-line text blocks - Variable Expansion: Variables expand by default in heredocs (perfect for templates)
- Quoted Delimiter: Use
<<'EOF'or<<\EOFto disable expansion - Here String:
<<< "text"for single-line input - Delimiter Rules: Must match exactly, no spaces, on its own line
- Use Cases: Config files, SQL queries, script generation, email templates
- Common Delimiters: EOF, END, STOP (use descriptive names for clarity)
What's Next?
Excellent! You've completed the Strings & Text Processing module. Next, we'll move on to Operators & Arithmetic - learning how to perform mathematical operations, comparisons, logical operations, and test file properties. These are essential for conditionals and calculations in your scripts!
Enjoying these tutorials?