Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output a multiline string in Bash?

Tags:

bash

How can I output a multipline string in Bash without using multiple echo calls like so:

echo "usage: up [--level <n>| -n <levels>][--help][--version]" echo  echo "Report bugs to: " echo "up home page: " 

I'm looking for a portable way to do this, using only Bash builtins.

like image 818
helpermethod Avatar asked Jun 10 '12 15:06

helpermethod


People also ask

How do I echo multiple lines in bash?

To add multiple lines to a file with echo, use the -e option and separate each line with \n. When you use the -e option, it tells echo to evaluate backslash characters such as \n for new line. If you cat the file, you will realize that each entry is added on a new line immediately after the existing content.

How do you write a multiline bash script?

Using a backslash \ or « to print a bash multiline command If you're writing a multiline command for bash, then you must add a backslash (\) at the end of each line. For multiline comments, you have to use the HereDoc « tag.

How do you do multiline strings?

Use triple quotes to create a multiline string It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end. Anything inside the enclosing Triple quotes will become part of one multiline string.


2 Answers

Here documents are often used for this purpose.

cat << EOF usage: up [--level <n>| -n <levels>][--help][--version]  Report bugs to:  up home page: EOF 

They are supported in all Bourne-derived shells including all versions of Bash.

like image 176
Dennis Williamson Avatar answered Sep 19 '22 15:09

Dennis Williamson


or you can do this:

echo "usage: up [--level <n>| -n <levels>][--help][--version]  Report bugs to:  up home page: " 
like image 23
Chris Mohr Avatar answered Sep 22 '22 15:09

Chris Mohr