Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Here Document And Here String in Bash on Linux
Here documents and here strings are powerful Bash features that allow users to pass multi-line input to commands without using external files. These redirection mechanisms are essential for shell scripting, enabling clean and readable code for tasks involving multi-line text processing, configuration generation, and command automation.
What is a Here Document?
A here document (heredoc) is a type of input redirection that allows you to specify multiple lines of input for a command using the << operator followed by a delimiter. The input continues until a line containing only the delimiter is encountered.
Basic Here Document Syntax
command <<DELIMITER line 1 line 2 line 3 DELIMITER
Example Using cat with Here Document
cat <<EOF This is line 1 of the input. This is line 2 of the input. This is line 3 of the input. EOF
This is line 1 of the input. This is line 2 of the input. This is line 3 of the input.
Creating Files with Here Documents
#!/bin/bash # Create a configuration file cat > config.txt <<EOF database_host=localhost database_port=5432 database_name=myapp EOF
What is a Here String?
A here string uses the <<< operator to pass a single string (which can contain newlines) as input to a command. Unlike here documents, here strings are simpler and don't require delimiters.
Here String Syntax
command <<<"string content"
Example Using grep with Here String
grep 'line 2' <<<"line 1 line 2 line 3"
line 2
Practical Examples
Processing Data with awk
# Using here document with awk
awk '{print "Field 1:", $1}' <<EOF
apple red fruit
car blue vehicle
book thick object
EOF
Field 1: apple Field 1: car Field 1: book
Variable Substitution in Here Documents
#!/bin/bash name="John" age=30 # Variables are expanded by default cat <<EOF Hello, my name is $name I am $age years old EOF
Preventing Variable Expansion
# Use quoted delimiter to prevent expansion cat <<'EOF' Hello, my name is $name I am $age years old EOF
Advanced Features
| Feature | Here Document | Here String |
|---|---|---|
| Syntax | <<DELIMITER |
<<<"string" |
| Variable Expansion | Yes (unless delimiter quoted) | Yes |
| Leading Tab Removal | <<-DELIMITER |
No |
| Multi-line Support | Native | With or literal newlines |
Indented Here Documents
if true; then
cat <<-EOF
This text is indented
But leading tabs are removed
EOF
fi
Common Use Cases
Configuration file generation Creating config files with dynamic content
SQL queries Passing multi-line SQL statements to database clients
Email templates Generating email content with variable substitution
Script documentation Embedding help text or usage information
Data processing Feeding sample data to text processing commands
Conclusion
Here documents and here strings are essential Bash features for handling multi-line input elegantly. Here documents excel at creating large blocks of text with variable substitution, while here strings provide a concise way to pass simple multi-line strings to commands. Both features improve script readability and eliminate the need for temporary files in many scenarios.
