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
How to write multiple line strings using Bash with variables on Linux?
Bash supports multiple approaches for creating and handling multiline strings. This functionality is essential when working with formatted output, configuration files, or complex text processing tasks in shell scripts.
There are three primary methods to create multiline strings in Bash, each with its own advantages depending on the use case.
Method 1: Using
Escape Sequences
The simplest approach uses the newline character to separate lines within a string. This method works well for short strings and when you need precise control over formatting.
Example
#!/bin/bash approach1="First Line Text\nSecond Line Text\nThird Line Text" echo -e "$approach1"
First Line Text Second Line Text Third Line Text
Note: The -e flag with echo enables interpretation of backslash escapes like .
Method 2: Literal Multiline Strings
You can define multiline strings by literally breaking lines within double quotes. This approach is more readable for longer text blocks and preserves the exact formatting as written.
Example
#!/bin/bash approach2="First Line Text Second Line Text Third Line Text" echo "$approach2"
First Line Text Second Line Text Third Line Text
Important: Always enclose the variable in double quotes when echoing to preserve the newlines.
Method 3: Using Heredoc (Here Documents)
The Heredoc approach is most powerful for complex multiline strings, especially when dealing with formatted text, code blocks, or configuration files. It allows you to include special characters and maintains exact formatting.
Example
#!/bin/bash read -r -d '' MULTI_LINE_VAR_STRING << EOM First Line Text Second Line Text Third Line Text EOM echo "$MULTI_LINE_VAR_STRING"
First Line Text Second Line Text Third Line Text
Comparison of Methods
| Method | Best For | Advantages | Disadvantages |
|---|---|---|---|
|
Escape |
Short strings | Compact, inline definition | Hard to read for long text |
| Literal Multiline | Medium-length text | Visual clarity, simple syntax | Indentation affects output |
| Heredoc | Complex formatted text | Flexible, handles special chars | More verbose syntax |
Working with Variables in Multiline Strings
All three methods support variable interpolation when using double quotes:
#!/bin/bash name="John" age=25 # Method 1 with variables message1="Hello $name\nYou are $age years old" echo -e "$message1" # Method 2 with variables message2="Hello $name You are $age years old" echo "$message2"
Conclusion
Bash provides flexible options for creating multiline strings, from simple escapes to powerful Heredoc syntax. Choose the method that best fits your specific needs: escape sequences for simple cases, literal multiline for readability, and Heredoc for complex formatted text with variables.
