Assign variable using multiple lines

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











up vote
2
down vote

favorite












I have a function



f()
echo 777



and a variable to which I assign the "return value" of the function.



x=$(f)


Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.



x=
$(f)


Still works! But: keeping things tidy also means respecting the indentation, so that gives something like



if foo
x=
$(f)
fi


which does not work anymore due to whitespaces! Is there a good workaround for this?










share|improve this question

























    up vote
    2
    down vote

    favorite












    I have a function



    f()
    echo 777



    and a variable to which I assign the "return value" of the function.



    x=$(f)


    Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.



    x=
    $(f)


    Still works! But: keeping things tidy also means respecting the indentation, so that gives something like



    if foo
    x=
    $(f)
    fi


    which does not work anymore due to whitespaces! Is there a good workaround for this?










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a function



      f()
      echo 777



      and a variable to which I assign the "return value" of the function.



      x=$(f)


      Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.



      x=
      $(f)


      Still works! But: keeping things tidy also means respecting the indentation, so that gives something like



      if foo
      x=
      $(f)
      fi


      which does not work anymore due to whitespaces! Is there a good workaround for this?










      share|improve this question













      I have a function



      f()
      echo 777



      and a variable to which I assign the "return value" of the function.



      x=$(f)


      Very concise! However, in my real code, the variable and function names are quite a bit longer and the function also eats positional arguments, so that concise line above, gets very long. Since I like to keep things tidy, I would like to break the code above in two lines.



      x=
      $(f)


      Still works! But: keeping things tidy also means respecting the indentation, so that gives something like



      if foo
      x=
      $(f)
      fi


      which does not work anymore due to whitespaces! Is there a good workaround for this?







      bash newlines assignment






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 4 hours ago









      pfnuesel

      2,63941838




      2,63941838




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can wrap it in quotes:



          if foo; then
          x="
          $(f)"
          fi


          Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:



          if foo; then
          x=$(
          )$(f)
          fi


          Another solution:



          if foo; then
          set -- $(f)
          x=$*
          fi





          share|improve this answer










          New contributor




          Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
            – pfnuesel
            10 mins ago

















          up vote
          1
          down vote













          If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading - allows your code to be intended with tabs only.



          Something like



          if true; then
          read -d '' -r x <<-EOF
          $(f)
          EOF
          fi


          But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim by setting the spacing for tab to 4 spaces as :set tabstop=4



          You can see how the Tab is formatted in my script, by looking into it using sed



          $ sed -n l script.sh
          #!/usr/bin/env bash$
          $
          $
          f()$
          echo 777$
          $
          $
          if true; then$
          tread -d '' -r x <<-PERSON$
          t$(f)$
          tPERSON$
          fi$
          $
          echo $x$


          Notice the t characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF errors.






          share|improve this answer


















          • 1




            Very nice! Had to read it like three times, to see where x is assigned.
            – pfnuesel
            1 hour ago

















          up vote
          1
          down vote













          Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:



          unset args
          args+='arg1 '
          args+='arg2 '
          args+='arg3 '
          x=$(f $args)





          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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "",
            contentPolicyHtml: "",
            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%2f478804%2fassign-variable-using-multiple-lines%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You can wrap it in quotes:



            if foo; then
            x="
            $(f)"
            fi


            Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:



            if foo; then
            x=$(
            )$(f)
            fi


            Another solution:



            if foo; then
            set -- $(f)
            x=$*
            fi





            share|improve this answer










            New contributor




            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
              – pfnuesel
              10 mins ago














            up vote
            0
            down vote



            accepted










            You can wrap it in quotes:



            if foo; then
            x="
            $(f)"
            fi


            Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:



            if foo; then
            x=$(
            )$(f)
            fi


            Another solution:



            if foo; then
            set -- $(f)
            x=$*
            fi





            share|improve this answer










            New contributor




            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















            • The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
              – pfnuesel
              10 mins ago












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            You can wrap it in quotes:



            if foo; then
            x="
            $(f)"
            fi


            Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:



            if foo; then
            x=$(
            )$(f)
            fi


            Another solution:



            if foo; then
            set -- $(f)
            x=$*
            fi





            share|improve this answer










            New contributor




            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            You can wrap it in quotes:



            if foo; then
            x="
            $(f)"
            fi


            Note that the indent will show/mess things up if you basically do anything other than use it plainly unquoted, though. Alternatively, you can use a subshell, which will eat the indent, instead:



            if foo; then
            x=$(
            )$(f)
            fi


            Another solution:



            if foo; then
            set -- $(f)
            x=$*
            fi






            share|improve this answer










            New contributor




            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            share|improve this answer



            share|improve this answer








            edited 1 hour ago





















            New contributor




            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.









            answered 2 hours ago









            Crestwave

            262




            262




            New contributor




            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.





            New contributor





            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            Crestwave is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.











            • The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
              – pfnuesel
              10 mins ago
















            • The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
              – pfnuesel
              10 mins ago















            The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
            – pfnuesel
            10 mins ago




            The second and third solutions are nice! The first one not so much. I assume this is why you got a downvote, which is completely unnecessary. Thanks for your help!
            – pfnuesel
            10 mins ago












            up vote
            1
            down vote













            If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading - allows your code to be intended with tabs only.



            Something like



            if true; then
            read -d '' -r x <<-EOF
            $(f)
            EOF
            fi


            But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim by setting the spacing for tab to 4 spaces as :set tabstop=4



            You can see how the Tab is formatted in my script, by looking into it using sed



            $ sed -n l script.sh
            #!/usr/bin/env bash$
            $
            $
            f()$
            echo 777$
            $
            $
            if true; then$
            tread -d '' -r x <<-PERSON$
            t$(f)$
            tPERSON$
            fi$
            $
            echo $x$


            Notice the t characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF errors.






            share|improve this answer


















            • 1




              Very nice! Had to read it like three times, to see where x is assigned.
              – pfnuesel
              1 hour ago














            up vote
            1
            down vote













            If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading - allows your code to be intended with tabs only.



            Something like



            if true; then
            read -d '' -r x <<-EOF
            $(f)
            EOF
            fi


            But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim by setting the spacing for tab to 4 spaces as :set tabstop=4



            You can see how the Tab is formatted in my script, by looking into it using sed



            $ sed -n l script.sh
            #!/usr/bin/env bash$
            $
            $
            f()$
            echo 777$
            $
            $
            if true; then$
            tread -d '' -r x <<-PERSON$
            t$(f)$
            tPERSON$
            fi$
            $
            echo $x$


            Notice the t characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF errors.






            share|improve this answer


















            • 1




              Very nice! Had to read it like three times, to see where x is assigned.
              – pfnuesel
              1 hour ago












            up vote
            1
            down vote










            up vote
            1
            down vote









            If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading - allows your code to be intended with tabs only.



            Something like



            if true; then
            read -d '' -r x <<-EOF
            $(f)
            EOF
            fi


            But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim by setting the spacing for tab to 4 spaces as :set tabstop=4



            You can see how the Tab is formatted in my script, by looking into it using sed



            $ sed -n l script.sh
            #!/usr/bin/env bash$
            $
            $
            f()$
            echo 777$
            $
            $
            if true; then$
            tread -d '' -r x <<-PERSON$
            t$(f)$
            tPERSON$
            fi$
            $
            echo $x$


            Notice the t characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF errors.






            share|improve this answer














            If you are allowed to use here-docs, the following style works good. Quoting the here-doc string with a leading - allows your code to be intended with tabs only.



            Something like



            if true; then
            read -d '' -r x <<-EOF
            $(f)
            EOF
            fi


            But remember copy pasting the code from above doesn't work as Stack Exchange replaces tabs with spaces. You need to carefully type in the Tab character for the lines starting with the here-doc and the lines ending the here-doc. My vim configuration has mapped the tab character to 8 spaces. If you want to make it even neater, modify the spacing rule in vim by setting the spacing for tab to 4 spaces as :set tabstop=4



            You can see how the Tab is formatted in my script, by looking into it using sed



            $ sed -n l script.sh
            #!/usr/bin/env bash$
            $
            $
            f()$
            echo 777$
            $
            $
            if true; then$
            tread -d '' -r x <<-PERSON$
            t$(f)$
            tPERSON$
            fi$
            $
            echo $x$


            Notice the t characters in the here-doc string above. If your script looks any different than the above, you would see the whining unexpected EOF errors.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 hours ago

























            answered 3 hours ago









            Inian

            3,053822




            3,053822







            • 1




              Very nice! Had to read it like three times, to see where x is assigned.
              – pfnuesel
              1 hour ago












            • 1




              Very nice! Had to read it like three times, to see where x is assigned.
              – pfnuesel
              1 hour ago







            1




            1




            Very nice! Had to read it like three times, to see where x is assigned.
            – pfnuesel
            1 hour ago




            Very nice! Had to read it like three times, to see where x is assigned.
            – pfnuesel
            1 hour ago










            up vote
            1
            down vote













            Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:



            unset args
            args+='arg1 '
            args+='arg2 '
            args+='arg3 '
            x=$(f $args)





            share|improve this answer
























              up vote
              1
              down vote













              Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:



              unset args
              args+='arg1 '
              args+='arg2 '
              args+='arg3 '
              x=$(f $args)





              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:



                unset args
                args+='arg1 '
                args+='arg2 '
                args+='arg3 '
                x=$(f $args)





                share|improve this answer












                Why split the line at the equals sign? You can just set the arguments to the function in a separate variable:



                unset args
                args+='arg1 '
                args+='arg2 '
                args+='arg3 '
                x=$(f $args)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                ewatt

                31516




                31516



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478804%2fassign-variable-using-multiple-lines%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery