> ## Content Index
> Fetch the complete content index at: https://itsfoss.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Create Multiline Strings in Bash
- URL: https://itsfoss.com/bash-multiline-strings/
- Published: 2024-04-16T08:55:24.000Z
- Updated: 2024-04-16T08:55:24.000Z
- Description: Here's how you can handle multiline strings in bash scripts.
- Author: Sagar Sharma
- Tags: Bash Tips, #hide

When you think of [strings in bash](https://itsfoss.com/bash-strings/), you think of a word or a combination of a few words. Basically, a single line. 

But what if you want to use a multiline strings in bash? I mean you have some text that goes over several lines. 

Well, in this tutorial, I will walk you through two ways to create multiline strings in bash:

1. Using heredocs
2. Using printf

Let's start with the first one. 

## 1\. Using heredocs 

Heredocs allow you to create multiline strings by [using the cat command](https://itsfoss.com/cat-command/) and a delimiter after the redirection operator (`<<`). Sounds complex? Here's a simple syntax:

```
string_var=$(cat <<DELIMITER
This is a
multiline
string.
DELIMITER
)

echo "$string_var"
```

For example, here, I created a `str` variable and used `END` as a delimiter to create a multiline string:

```
str=$(cat <<END
Sagar Sharma
from 
itsFOSS.com 
END
)

echo "$str"

```

If you execute the above script, the output will look like this:

![Use heredocs to create multiline strings in bash](https://itsfoss.com/content/images/2024/04/Use-heredocs-to-create-multiline-strings-in-bash-1.png)

## 2\. Using printf 

With the printf command, you can print multiline strings by using a newline character (`\n`) as shown here:

```
multiline_string="String-1\nString-2\nString-3."
```

Once after assigning multiline strings to a variable, you use the printf command with the `-e` flag to enable the interpretation of backslash escape sequences.

```
multiline_string="String-1\nString-2\nString-3."
printf -e "$multiline_string"
```

For example, here, I created `str_var` to hold 3 multiline strings and then execute the script to show its outcome:

```
str_var="Greetings\nFrom\nSagar Sharma."
echo -e "$str_var"
```

![Use the printf command to print multiline strings in bash](https://itsfoss.com/content/images/2024/04/Use-the-printf-command-to-print-multiline-strings-in-bash.png)

## More on strings in bash 

Ever wondered how you split a string in bash? Well, to help you with this, we wrote a [detailed guide on how to split strings in bash](https://itsfoss.com/bash-split-string/):

[Bash Split String Example ExplainedHere’s a quick little bash tip about splitting strings based on a specified delimiter.![](https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png)It's FOSSPratham Patel![](https://itsfoss.com/content/images/2023/09/split-string-in-bash.png)](https://itsfoss.com/bash-split-string/)

But if you are a beginner and want to [know the basics of handling strings in bash](https://itsfoss.com/bash-strings/), then refer to the following tutorial:

[Bash Basics Series #6: Handling String OperationsIn this chapter of the Bash Basics series, learn to perform various common string operations like extracting, replacing and deleting substrings.![](https://itsfoss.com/content/images/size/w256h256/2022/12/android-chrome-192x192.png)It's FOSSAbhishek Prakash![](https://itsfoss.com/content/images/2023/07/bash-series-6-strings.png)](https://itsfoss.com/bash-strings/)

I hope you will find this guide helpful.