- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to write multiple line strings using Bash with variables on Linux?
Setting a variable to a single line in bash and then printing it to console is a fairly easy process, but if we want to write multiple line strings using Bash then we have to consider different approaches.
In total there are three approaches that we can make use of, all of these are mentioned below with examples.
Multiline with
We can make use of the
symbol to make sure that whatever string we write has a newline in between them. With this approach we can write as many lines as possible, we just need to write the same number of
’s in the string.
Example
approach1="First Line Text
Second Line Text
Third Line Text" echo $approach1
Output
sh-3.2# ./sample.sh First Line Text Second Line Text Third Line Text
Multiline String
Just make sure to put the entire string in double quotes.
Example
approach2="First Line Text Second Line Text Third Line Text" echo "$approach2"
Output
sh-3.2# ./sample.sh First Line Text Second Line Text Third Line Text
Heredoc
Use the Heredoc approach.
Example
read -r -d '' MULTI_LINE_VAR_STRING << EOM First Line Text Second Line Text Third Line Text EOM echo $MULTI_LINE_VAR_STRING
Output
sh-3.2# ./sample.sh First Line Text Second Line Text Third Line Text
- Related Articles
- Preserve Bash History in Multiple Terminal Windows on Linux
- How to Clear BASH Command Line History in Linux?
- How to concatenate multiple C++ strings on one line?
- How to create a CPU spike with bash command on Linux?
- Introduction to Bash Globbing on Linux
- How to replace spaces in file names using bash script on Linux?
- How to split strings on multiple delimiters with Python?
- Extracting a substring using Linux bash
- Implement a Counter in Bash Script on Linux
- Exclude Multiple Patterns With Grep on Linux
- How to match two strings that are present in one line with grep in Linux?
- Count lines in a file using Linux bash
- Here Document And Here String in Bash on Linux
- The Meaning of IFS in Bash Scripting on Linux
- Check if Directory is Mounted in Bash on Linux

Advertisements