How to split strings over multiple lines in Bash? How to split strings over multiple lines in Bash? bash bash

How to split strings over multiple lines in Bash?


This is what you may want

$       echo "continuation"\>       "lines"continuation lines

If this creates two arguments to echo and you only want one, then let's look at string concatenation. In bash, placing two strings next to each other concatenate:

$ echo "continuation""lines"continuationlines

So a continuation line without an indent is one way to break up a string:

$ echo "continuation"\> "lines"continuationlines

But when an indent is used:

$       echo "continuation"\>       "lines"continuation lines

You get two arguments because this is no longer a concatenation.

If you would like a single string which crosses lines, while indenting but not getting all those spaces, one approach you can try is to ditch the continuation line and use variables:

$ a="continuation"$ b="lines"$ echo $a$bcontinuationlines

This will allow you to have cleanly indented code at the expense of additional variables. If you make the variables local it should not be too bad.


Here documents with the <<-HERE terminator work well for indented multi-line text strings. It will remove any leading tabs from the here document. (Line terminators will still remain, though.)

cat <<-____HERE    continuation    lines____HERE

See also http://ss64.com/bash/syntax-here.html

If you need to preserve some, but not all, leading whitespace, you might use something like

sed 's/^  //' <<____HERE    This has four leading spaces.    Two of them will be removed by sed.____HERE

or maybe use tr to get rid of newlines:

tr -d '\012' <<-____    continuation     lines____

(The second line has a tab and a space up front; the tab will be removed by the dash operator before the heredoc terminator, whereas the space will be preserved.)

For wrapping long complex strings over many lines, I like printf:

printf '%s' \    "This will all be printed on a " \    "single line (because the format string " \    "doesn't specify any newline)"

It also works well in contexts where you want to embed nontrivial pieces of shell script in another language where the host language's syntax won't let you use a here document, such as in a Makefile or Dockerfile.

printf '%s\n' >./myscript \    '#!/bin/sh` \    "echo \"G'day, World\"" \    'date +%F\ %T' && \chmod a+x ./myscript && \./myscript


You can use bash arrays

$ str_array=("continuation"             "lines")

then

$ echo "${str_array[*]}"continuation lines

there is an extra space, because (after bash manual):

If the word is double-quoted, ${name[*]} expands to a single word with the value of each array member separated by the first character of the IFS variable

So set IFS='' to get rid of extra space

$ IFS=''$ echo "${str_array[*]}"continuationlines