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?

  • 2
    Can you provide the data in your script and add it your question? – Nasir Riley Feb 28 at 15:28
  • 1
    What operation does the script perform on the variable? How are you splitting it? What is the error exactly? – steeldriver Feb 28 at 15:33
  • I am assigning an SQL script to that variable and thereafter running the psql command to execute the SQL script on postgresql database. The SQL script which is assigned to the variable contains around 50 t0 75 lines. – Srinivas Kamalanathan Attipatt Feb 28 at 16:28
  • 1
    @SrinivasKamalanathanAttipatt please show - don't tell. In particular, things like how you are assigning values and whether you are properly quoting the string expansion may affect the answer. You can use the edit button to revise your question. – steeldriver Feb 28 at 17:11
  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you! – Jeff Schaller Mar 17 at 16:32

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


    '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

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.