How do you use bash continuation lines?
I realize that you can do this:
echo "continuation \
lines"
>continuation lines
However, if you have indented code, it doesn't work out so well:
echo "continuation \
lines"
>continuation lines
I came across a situation in which I had to send a long message as part of a command argument and had to adhere to the line length limitation. The commands looks something like this:
The way I solved this is to move the message out as a here document (like @tripleee suggested). But a here document becomes a stdin, so it needs to be read back in, I went with the below approach:
This has the advantage that
$message
can be used exactly as the string constant with no extra whitespace or line breaks.Note that the actual message lines above are prefixed with a
tab
character each, which is stripped by here document itself (because of the use of<<-
). There are still line breaks at the end, which are then replaced bydd
with spaces.Note also that if you don't remove newlines, they will appear as is when
"$message"
is expanded. In some cases, you may be able to workaround by removing the double-quotes around$message
, but the message will no longer be a single argument.This isn't exactly what the user asked, but another way to create a long string that spans multiple lines is by incrementally building it up, like so:
Obviously in this case it would have been simpler to build it one go, but this style can be very lightweight and understandable when dealing with longer strings.
This probably doesn't really answer your question but you might find it useful anyway.
The first command creates the script that's displayed by the second command.
The third command makes that script executable.
The fourth command provides a usage example.
Note that if you really want to use this script, it makes more sense to move the executable script into
~/bin
so that it will be in your path.Check the python reference for details on how
textwrap.dedent
works.If the usage of
$'...'
or"$(...)"
is confusing to you, ask another question (one per construct) if there's not already one up. It might be nice to provide a link to the question you find/ask so that other people will have a linked reference.You could simply separate it with newlines (without using backslash) as required within the indentation as follows and just strip of new lines.
Example:
Or if it is a variable definition newlines gets automatically converted to spaces. So, strip of extra spaces only if applicable.
This is what you may want
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:
So a continuation line without an indent is one way to break up a string:
But when an indent is used:
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:
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.)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
For wrapping long complex strings over many lines, I like
printf
: