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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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?










share|improve this question



















  • 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















0















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?










share|improve this question



















  • 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













0












0








0








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?










share|improve this question
















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?







linux bash variable text-formatting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 28 at 17:15









Rui F Ribeiro

41.8k1483142




41.8k1483142










asked Feb 28 at 15:25









Srinivas Kamalanathan AttipattSrinivas Kamalanathan Attipatt

1




1







  • 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












  • 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







2




2





Can you provide the data in your script and add it your question?

– Nasir Riley
Feb 28 at 15:28






Can you provide the data in your script and add it your question?

– Nasir Riley
Feb 28 at 15:28





1




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





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





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




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





@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





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










3 Answers
3






active

oldest

votes


















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 '%sn' "$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 '%sn' "$text[*]" | fmt -w 45





share|improve this answer
































    0














    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 '%sn' "$x"
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.





    share|improve this answer






























      0














      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|improve this answer






















        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503572%2fhow-to-break-a-long-string-into-multiple-lines-assigned-to-a-variable-in-linux-b%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        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 '%sn' "$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 '%sn' "$text[*]" | fmt -w 45





        share|improve this answer





























          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 '%sn' "$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 '%sn' "$text[*]" | fmt -w 45





          share|improve this answer



























            1












            1








            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 '%sn' "$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 '%sn' "$text[*]" | fmt -w 45





            share|improve this answer















            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 '%sn' "$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 '%sn' "$text[*]" | fmt -w 45






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered Mar 18 at 9:59









            KusalanandaKusalananda

            138k17258426




            138k17258426























                0














                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 '%sn' "$x"
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.





                share|improve this answer



























                  0














                  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 '%sn' "$x"
                  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.





                  share|improve this answer

























                    0












                    0








                    0







                    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 '%sn' "$x"
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.





                    share|improve this answer













                    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 '%sn' "$x"
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 28 at 15:31









                    Jeff SchallerJeff Schaller

                    44k1161142




                    44k1161142





















                        0














                        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|improve this answer



























                          0














                          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|improve this answer

























                            0












                            0








                            0







                            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|improve this answer













                            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|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 28 at 17:52









                            ilkkachuilkkachu

                            62.8k10103180




                            62.8k10103180



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f503572%2fhow-to-break-a-long-string-into-multiple-lines-assigned-to-a-variable-in-linux-b%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown






                                Popular posts from this blog

                                Peggy Mitchell

                                Palaiologos

                                The Forum (Inglewood, California)