How to break a long string into multiple lines assigned to a variable in linux bash script

11,363

Solution 1

Assigning long strings as multiple sub-string in an array could make the code more aesthetically appealing:

#!/bin/bash

text=(
    'Contrary to popular'
    'belief, Lorem Ipsum'
    'is not simply'
    'random text. It has'
    'roots in a piece'
    'of classical Latin'
    'literature from 45'
    'BC, making it over'
    '2000 years old.'
)

# output one line per string in the array:
printf '%s\n' "${text[@]}"

# output all strings on a single line, delimited by space (first
# character of $IFS), and let "fmt" format it to 45 characters per line
printf '%s\n' "${text[*]}" | fmt -w 45

Solution 2

One suggestion:

x='Lorem ipsum dolor sit amet, consectetur '\
'adipiscing elit, sed do eiusmod tempor '\
'incididunt ut labore et dolore magna aliqua.'

Which results in the expected:

$ printf '%s\n' "$x"
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Solution 3

Assuming you want to assign the string some long piece of text to assign to the variable str. This won't work:

str='some long'
'piece of text'
'to assign'

It'll try to run the lines after the first as commands, you'll probably get "command not found" errors.

You can do this, but the newlines will be embedded in the variable, so it won't be a single line:

str='some long
piece of text
to assign'

Though you can use the substring replacement expansion (in Bash, ksh, zsh) to replace them with spaces, e.g. str="${str//$'\n'/ }" to do the replacement and save the new value in the same variable. Note that any trailing whitespace on all but the last line will be left in the string.

Another option is to use += to append to the value of the variable (also Bash, ksh, zsh only):

str='some long'
str+=' piece of text'
str+=' to assign'

Here, any whitespace will need to be manually typed within the quotes.

Or, similarly in a standard shell:

str='some long'
str="$str"' piece of text'
str="$str"' to assign'

Then there's the way with line continuation (that Jeff already mentioned in their answer):

str='some long'\
' piece of text'\
' to assign'

Here, too, trailing whitespace is important, the line continuation only works if the backslash is immediately followed by a newline, not if there are spaces in between.

Share:
11,363

Related videos on Youtube

Srinivas Kamalanathan Attipatt
Author by

Srinivas Kamalanathan Attipatt

Updated on September 18, 2022

Comments

  • Srinivas Kamalanathan Attipatt
    Srinivas Kamalanathan Attipatt over 1 year

    I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple lines and assigned to a variable?

  • Amit Naidu
    Amit Naidu almost 5 years
    Suppose I want to do this, but also maintain my nesting and indentation ?
  • ilkkachu
    ilkkachu almost 5 years
    @AmitNaidu, what nesting an indentation? There's no such mentioned in this question, and without a better description, there's no way to know what exactly you're doing. Please post a new question if you have a new question.
  • mattia.b89
    mattia.b89 over 3 years
    please use proper syntax