How to split strings over multiple lines in Bash?

How to split strings over multiple lines in Bash?

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

What to do with continuation lines in Bash?

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.

How to read line by line in Bash?

ProcessText1 needs to call ProcessLine at two places, which might be uncomfortable if you would like to place a block of code there which directly processes the line, instead of calling a function which processes the line; you would have to repeat the code which is error-prone.

How to specify a multi-line shell variable?

However, this post comes up among the first results when searching for multi-line shell variables and an additional answer seemed appropriate. Thanks to dimo414’s answer to a similar question, this shows how his great solution works, and shows that you can have quotes and variables in the text easily as well:

Is it possible to split a long string argument?

Given a command that takes a single long string argument like: is it possible to somehow split it in a way similar to how separate arguments can be split with \\. mycommand -arg1 “very \\ long \\ string \\ which …”

What do you need to know about splitting a string?

Now one thing to watch out for is the location of split of a string. This might be a single character or even combination of multiple characters. The location or the pattern on which it is decided to split the string is known as delimiter.

How to pass a string as an argument in Bash?

The string is passed as an argument. E.g. $ {2} == “cat cat file”. How can I loop through it? Also, how can I check if a string contains spaces? Did you try just passing the string variable to a for loop? Bash, for one, will split on whitespace automatically. sentence=”This is a sentence.” for word in $sentence do echo $word done