Bash: split long string argument to multiple lines?

You can assign your string to a variable like this:

long_arg="my very long string\
 which does not fit\
 on the screen"

Then just use the variable:

mycommand "$long_arg"

Within double quotes, a newline preceded by a backslash is removed. Note that all the other white space in the string is significant, i.e. it will be present in the variable.


Have you tried without the quotes?

$ foo() { echo -e "1-$1\n2-$2\n3-$3"; }

$ foo "1 \
2 \
3"

1-1 2 3
2-
3-

$ foo 1 \
2 \ 
3

1-1
2-2
3-3

When you encapsulate it in double-quotes, it's honoring your backslash and ignoring the following character, but since you're wrapping the whole thing in quotes, it's making it think that the entire block of text within the quotes should be treated as a single argument.

Tags:

Bash

Multiline