How to Create Multi-Line String in Bash
-
Use
here-document
to Make Multi-Line String in Bash - Use Shell Variable to Make Multi-Line String in Bash
-
Use
printf
to Make Multi-Line String in Bash -
Use
echo
With the-e
Option to Make Multi-Line String in Bash -
Use
echo
to Make Multi-Line String in Bash
This tutorial demonstrates different ways to print a multi-line string to a file in bash without putting extra space (indentation) by the use of here-document
, shell variable, printf
, echo
, and echo
with -e
option.
Use here-document
to Make Multi-Line String in Bash
Here-document
provides an interactive way to input multi-line string into a file. The EOF
is known as the Here Tag
. The Here Tag
tells the shell that you will input a multi-line string until the Here Tag
since it acts as a delimiter. The <<
is used to set the Here Tag
. The >
is used for input redirection. It redirects the input to the specified file, output.txt
, in our case.
cat << EOF > output.txt
> Hello
> World
> !
> EOF
Let us check the content of the output.txt
file with the cat
command.
cat output.txt
From the output, we see that every set of words has its own line, and there are no extra spaces.
Hello
World
!
Use Shell Variable to Make Multi-Line String in Bash
Here, we are using a shell variable named greet
. We have assigned a multi-line string to greet
.
greet="Hello
> ,
> wolrd
> !"
The command below gets the multi-line string in the shell variable, greet
, and redirects it to the specified file, multiline.txt
, using >
.
echo "$greet" > multiline.txt
Check the content of the multiline.txt
with the cat
command.
cat multiline.txt
From the output, we see that every set of words has its own line, and there are no extra spaces.
Hello
,
wolrd
!
Use printf
to Make Multi-Line String in Bash
We can use printf
with the new line character and redirect the output to a file using >
. The content in the file does not have extra spaces.
#!/bin/bash
printf "Every word is on a seperate line!\n"
printf "%s\n" "Hello" "," "World" "!" > multiline.txt
Output:
Every word is on a separate line!
Print out the content of multiline.txt
with the cat
command.
cat multiline.txt
From the output, we see that every set of words has its own line, and there are no extra spaces.
Hello
,
World
!
Use echo
With the -e
Option to Make Multi-Line String in Bash
The following bash script prints the words to multiline.txt
without any extra spaces. The -e
option enables the interpretation of escape characters in the variable greet
.
#!/bin/bash
greet="Hello\n,\nWorld\n!"
echo -e $greet > multiline.txt
Print out the content of multiline.txt
with the cat
command
cat multiline.txt
From the output, we see that every set of words has its own line, and there are no extra spaces.
Hello
,
World
!
Use echo
to Make Multi-Line String in Bash
The script below assigns a multi-line string to a variable named greet
. Next, the content of the variable is redirected to the multiline.txt
files using >
. The quotes on the greet
variable preserve the new lines.
#!/bin/bash
greet="Hello
,
World
!"
echo "$greet" > multiline.txt
Show content of multiline.txt
with cat
command.
cat multiline.txt
From the output, we see that every set of words has its own line, and there are no extra spaces.
Hello
,
World
!