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.
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.
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.
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.
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.
or you can do this:
echo "usage: up [--level <n>| -n <levels>][--help][--version] Report bugs to: up home page: "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With