Advanced Bash Scripting
Streams
Heredocs
Heredocs let you include multi-line text or commands directly within a Bash script, avoiding repetitive echo
statements and manual editing. They’re invaluable for generating configuration files, embedding SQL queries, or executing batches of commands over SSH—especially in non-interactive environments where editors like Vim or Nano aren’t available.
Note
A Heredoc sends an entire block of text as input to a command. This keeps your scripts clean and reduces the risk of accidental overwrites when using >
vs. >>
.
Why Choose Heredocs Over Multiple echo
Commands
Using individual echo
lines quickly becomes error-prone as your script grows:
echo "line1" > file.txt # creates or overwrites file.txt
echo "line2" >> file.txt # appends to file.txt
echo "line3" >> file.txt # appends to file.txt
If you mistakenly use >
instead of >>
, you can overwrite your file. Heredocs treat the block as a single unit of input, eliminating this risk.
Basic Heredoc Syntax
Feed a block of text into any command—here, cat
—using the <<DELIM
operator:
cat <<EOF
line1
line2
line3
EOF
Output:
line1
line2
line3
To write directly to a file, redirect the command’s output:
cat > file.txt <<EOF
line1
line2
line3
EOF
Tips for smooth usage:
- The closing delimiter must match exactly (case-sensitive) and have no leading spaces.
- Common delimiters include
EOF
,EOM
, or a custom token of your choice.
cat <<CUSTOM
alpha
beta
gamma
CUSTOM
Warning
If you mistype the closing token, the shell will continue to prompt for input until you either provide the correct delimiter or cancel with Ctrl+C
.
Common Use Cases for Heredocs
Use Case | Example Command |
---|---|
Generate configuration files | cat > app.conf <<EOF ... EOF |
Embed SQL scripts | psql <<SQL ... SQL |
Batch SSH commands | ssh user@host <<EOF ... EOF |
Create Dockerfiles on the fly | docker build - <<EOF ... EOF |
Quick Checklist: Heredoc Benefits
- One-liner for simple operations
- Embed large blocks of text without quotes
- Support complex scripting scenarios
Remote Command Execution via SSH
You can run multiple commands on a remote host in one go. First, check your remote directory:
ssh [email protected] ls
# Example output:
# guard_clause pid shebang
Next, supply commands through a Heredoc:
ssh [email protected] <<EOF
mkdir -p ~/heredocs
echo "Sample content for Heredocs file" > ~/heredocs/heredocsfile.txt
EOF
Verify the result:
ssh [email protected] <<EOF
ls ~/heredocs
cat ~/heredocs/heredocsfile.txt
EOF
Expected output:
heredocsfile.txt
Sample content for Heredocs file
Here Strings for Single-Line Input
When you need to pass a single line of text into a command, a Here String (<<<
) is more concise:
export MESSAGE="Hello from Here String"
cat <<<"$MESSAGE"
# Or directly:
cat <<<"Hello World"
Here Strings avoid quoting headaches and extra redirection symbols for small data payloads.
Links and References
Feel free to explore these resources to deepen your understanding of shell scripting techniques and automation workflows.
Watch Video
Watch video content
Practice Lab
Practice lab