Assign variable using multiple lines












4















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



























    4















    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

























      4












      4








      4








      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 Oct 31 '18 at 3:26









      pfnueselpfnuesel

      2,75442240




      2,75442240






















          4 Answers
          4






          active

          oldest

          votes


















          3














          You can store the value in $_, which is set to the last argument:



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


          Or can use a subshell to eat the indent:



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





          share|improve this answer





















          • 1





            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
            Oct 31 '18 at 7:15











          • @pfnuesel It's a long story; basically, I thought of it first, posted it, realized the problem and added the note, got downvoted while thinking/searching for other solutions, then added the others. I'm not entirely sure why I didn't delete it after that (partially because the subshell was unquoted, anyway, I guess), but I've removed it now. Thanks for the accept!

            – Crestwave
            Oct 31 '18 at 8:11













          • Why use two subshells if you already have one?

            – Ruslan
            Oct 31 '18 at 10:51











          • @Ruslan Well, it crossed my mind, but then it slipped right out of it. You want Gilles' answer.

            – Crestwave
            Oct 31 '18 at 12:16



















          11














          Why go for complex, hard-to-read constructs? There is a perfectly natural way to present this which doesn't need any intermediate assignments, fancy ways of building an empty string, quoting subtleties or other cognitive burden.



          if foo; then
          x=$(
          a_very_long_command_name --option1='argument 1 is long'
          --option2='argument 2 is long as well'
          )
          fi





          share|improve this answer
























          • This! To put the opening parenthesis before the line break is what I see in style guides for most programming languages anyway. Look at these examples from PEP-8 for instance.

            – David Foerster
            Oct 31 '18 at 9:26











          • So obvious. Funny how we all got very creative, while the solution was this simple.

            – pfnuesel
            Oct 31 '18 at 14:34



















          2














          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





















          • 2





            Very nice! Had to read it like three times, to see where x is assigned.

            – pfnuesel
            Oct 31 '18 at 6:23



















          2














          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
























          • This doesn't work if the arguments contain whitespace or characters that the shell interprets as wildcards. You can't store a list of arguments in a string variable.

            – Gilles
            Oct 31 '18 at 8:46











          • Considering that this is about Bash you could use an actual array to deal with escaping issues: cmd=(f /*line break*/ "arg1" /*line break*/ "arg2" ... ); x="$("${cmd[@]}")"

            – David Foerster
            Oct 31 '18 at 9:20













          • He said the arguments are positional. Whitespace would break it anyways.

            – ewatt
            Oct 31 '18 at 13:19












          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%2f478804%2fassign-variable-using-multiple-lines%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You can store the value in $_, which is set to the last argument:



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


          Or can use a subshell to eat the indent:



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





          share|improve this answer





















          • 1





            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
            Oct 31 '18 at 7:15











          • @pfnuesel It's a long story; basically, I thought of it first, posted it, realized the problem and added the note, got downvoted while thinking/searching for other solutions, then added the others. I'm not entirely sure why I didn't delete it after that (partially because the subshell was unquoted, anyway, I guess), but I've removed it now. Thanks for the accept!

            – Crestwave
            Oct 31 '18 at 8:11













          • Why use two subshells if you already have one?

            – Ruslan
            Oct 31 '18 at 10:51











          • @Ruslan Well, it crossed my mind, but then it slipped right out of it. You want Gilles' answer.

            – Crestwave
            Oct 31 '18 at 12:16
















          3














          You can store the value in $_, which is set to the last argument:



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


          Or can use a subshell to eat the indent:



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





          share|improve this answer





















          • 1





            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
            Oct 31 '18 at 7:15











          • @pfnuesel It's a long story; basically, I thought of it first, posted it, realized the problem and added the note, got downvoted while thinking/searching for other solutions, then added the others. I'm not entirely sure why I didn't delete it after that (partially because the subshell was unquoted, anyway, I guess), but I've removed it now. Thanks for the accept!

            – Crestwave
            Oct 31 '18 at 8:11













          • Why use two subshells if you already have one?

            – Ruslan
            Oct 31 '18 at 10:51











          • @Ruslan Well, it crossed my mind, but then it slipped right out of it. You want Gilles' answer.

            – Crestwave
            Oct 31 '18 at 12:16














          3












          3








          3







          You can store the value in $_, which is set to the last argument:



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


          Or can use a subshell to eat the indent:



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





          share|improve this answer















          You can store the value in $_, which is set to the last argument:



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


          Or can use a subshell to eat the indent:



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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 7 at 11:56

























          answered Oct 31 '18 at 4:59









          CrestwaveCrestwave

          685




          685








          • 1





            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
            Oct 31 '18 at 7:15











          • @pfnuesel It's a long story; basically, I thought of it first, posted it, realized the problem and added the note, got downvoted while thinking/searching for other solutions, then added the others. I'm not entirely sure why I didn't delete it after that (partially because the subshell was unquoted, anyway, I guess), but I've removed it now. Thanks for the accept!

            – Crestwave
            Oct 31 '18 at 8:11













          • Why use two subshells if you already have one?

            – Ruslan
            Oct 31 '18 at 10:51











          • @Ruslan Well, it crossed my mind, but then it slipped right out of it. You want Gilles' answer.

            – Crestwave
            Oct 31 '18 at 12:16














          • 1





            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
            Oct 31 '18 at 7:15











          • @pfnuesel It's a long story; basically, I thought of it first, posted it, realized the problem and added the note, got downvoted while thinking/searching for other solutions, then added the others. I'm not entirely sure why I didn't delete it after that (partially because the subshell was unquoted, anyway, I guess), but I've removed it now. Thanks for the accept!

            – Crestwave
            Oct 31 '18 at 8:11













          • Why use two subshells if you already have one?

            – Ruslan
            Oct 31 '18 at 10:51











          • @Ruslan Well, it crossed my mind, but then it slipped right out of it. You want Gilles' answer.

            – Crestwave
            Oct 31 '18 at 12:16








          1




          1





          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
          Oct 31 '18 at 7:15





          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
          Oct 31 '18 at 7:15













          @pfnuesel It's a long story; basically, I thought of it first, posted it, realized the problem and added the note, got downvoted while thinking/searching for other solutions, then added the others. I'm not entirely sure why I didn't delete it after that (partially because the subshell was unquoted, anyway, I guess), but I've removed it now. Thanks for the accept!

          – Crestwave
          Oct 31 '18 at 8:11







          @pfnuesel It's a long story; basically, I thought of it first, posted it, realized the problem and added the note, got downvoted while thinking/searching for other solutions, then added the others. I'm not entirely sure why I didn't delete it after that (partially because the subshell was unquoted, anyway, I guess), but I've removed it now. Thanks for the accept!

          – Crestwave
          Oct 31 '18 at 8:11















          Why use two subshells if you already have one?

          – Ruslan
          Oct 31 '18 at 10:51





          Why use two subshells if you already have one?

          – Ruslan
          Oct 31 '18 at 10:51













          @Ruslan Well, it crossed my mind, but then it slipped right out of it. You want Gilles' answer.

          – Crestwave
          Oct 31 '18 at 12:16





          @Ruslan Well, it crossed my mind, but then it slipped right out of it. You want Gilles' answer.

          – Crestwave
          Oct 31 '18 at 12:16













          11














          Why go for complex, hard-to-read constructs? There is a perfectly natural way to present this which doesn't need any intermediate assignments, fancy ways of building an empty string, quoting subtleties or other cognitive burden.



          if foo; then
          x=$(
          a_very_long_command_name --option1='argument 1 is long'
          --option2='argument 2 is long as well'
          )
          fi





          share|improve this answer
























          • This! To put the opening parenthesis before the line break is what I see in style guides for most programming languages anyway. Look at these examples from PEP-8 for instance.

            – David Foerster
            Oct 31 '18 at 9:26











          • So obvious. Funny how we all got very creative, while the solution was this simple.

            – pfnuesel
            Oct 31 '18 at 14:34
















          11














          Why go for complex, hard-to-read constructs? There is a perfectly natural way to present this which doesn't need any intermediate assignments, fancy ways of building an empty string, quoting subtleties or other cognitive burden.



          if foo; then
          x=$(
          a_very_long_command_name --option1='argument 1 is long'
          --option2='argument 2 is long as well'
          )
          fi





          share|improve this answer
























          • This! To put the opening parenthesis before the line break is what I see in style guides for most programming languages anyway. Look at these examples from PEP-8 for instance.

            – David Foerster
            Oct 31 '18 at 9:26











          • So obvious. Funny how we all got very creative, while the solution was this simple.

            – pfnuesel
            Oct 31 '18 at 14:34














          11












          11








          11







          Why go for complex, hard-to-read constructs? There is a perfectly natural way to present this which doesn't need any intermediate assignments, fancy ways of building an empty string, quoting subtleties or other cognitive burden.



          if foo; then
          x=$(
          a_very_long_command_name --option1='argument 1 is long'
          --option2='argument 2 is long as well'
          )
          fi





          share|improve this answer













          Why go for complex, hard-to-read constructs? There is a perfectly natural way to present this which doesn't need any intermediate assignments, fancy ways of building an empty string, quoting subtleties or other cognitive burden.



          if foo; then
          x=$(
          a_very_long_command_name --option1='argument 1 is long'
          --option2='argument 2 is long as well'
          )
          fi






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 31 '18 at 8:49









          GillesGilles

          545k12911071622




          545k12911071622













          • This! To put the opening parenthesis before the line break is what I see in style guides for most programming languages anyway. Look at these examples from PEP-8 for instance.

            – David Foerster
            Oct 31 '18 at 9:26











          • So obvious. Funny how we all got very creative, while the solution was this simple.

            – pfnuesel
            Oct 31 '18 at 14:34



















          • This! To put the opening parenthesis before the line break is what I see in style guides for most programming languages anyway. Look at these examples from PEP-8 for instance.

            – David Foerster
            Oct 31 '18 at 9:26











          • So obvious. Funny how we all got very creative, while the solution was this simple.

            – pfnuesel
            Oct 31 '18 at 14:34

















          This! To put the opening parenthesis before the line break is what I see in style guides for most programming languages anyway. Look at these examples from PEP-8 for instance.

          – David Foerster
          Oct 31 '18 at 9:26





          This! To put the opening parenthesis before the line break is what I see in style guides for most programming languages anyway. Look at these examples from PEP-8 for instance.

          – David Foerster
          Oct 31 '18 at 9:26













          So obvious. Funny how we all got very creative, while the solution was this simple.

          – pfnuesel
          Oct 31 '18 at 14:34





          So obvious. Funny how we all got very creative, while the solution was this simple.

          – pfnuesel
          Oct 31 '18 at 14:34











          2














          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





















          • 2





            Very nice! Had to read it like three times, to see where x is assigned.

            – pfnuesel
            Oct 31 '18 at 6:23
















          2














          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





















          • 2





            Very nice! Had to read it like three times, to see where x is assigned.

            – pfnuesel
            Oct 31 '18 at 6:23














          2












          2








          2







          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 Oct 31 '18 at 4:41

























          answered Oct 31 '18 at 4:19









          InianInian

          5,2051529




          5,2051529








          • 2





            Very nice! Had to read it like three times, to see where x is assigned.

            – pfnuesel
            Oct 31 '18 at 6:23














          • 2





            Very nice! Had to read it like three times, to see where x is assigned.

            – pfnuesel
            Oct 31 '18 at 6:23








          2




          2





          Very nice! Had to read it like three times, to see where x is assigned.

          – pfnuesel
          Oct 31 '18 at 6:23





          Very nice! Had to read it like three times, to see where x is assigned.

          – pfnuesel
          Oct 31 '18 at 6:23











          2














          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
























          • This doesn't work if the arguments contain whitespace or characters that the shell interprets as wildcards. You can't store a list of arguments in a string variable.

            – Gilles
            Oct 31 '18 at 8:46











          • Considering that this is about Bash you could use an actual array to deal with escaping issues: cmd=(f /*line break*/ "arg1" /*line break*/ "arg2" ... ); x="$("${cmd[@]}")"

            – David Foerster
            Oct 31 '18 at 9:20













          • He said the arguments are positional. Whitespace would break it anyways.

            – ewatt
            Oct 31 '18 at 13:19
















          2














          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
























          • This doesn't work if the arguments contain whitespace or characters that the shell interprets as wildcards. You can't store a list of arguments in a string variable.

            – Gilles
            Oct 31 '18 at 8:46











          • Considering that this is about Bash you could use an actual array to deal with escaping issues: cmd=(f /*line break*/ "arg1" /*line break*/ "arg2" ... ); x="$("${cmd[@]}")"

            – David Foerster
            Oct 31 '18 at 9:20













          • He said the arguments are positional. Whitespace would break it anyways.

            – ewatt
            Oct 31 '18 at 13:19














          2












          2








          2







          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 Oct 31 '18 at 5:22









          ewattewatt

          37028




          37028













          • This doesn't work if the arguments contain whitespace or characters that the shell interprets as wildcards. You can't store a list of arguments in a string variable.

            – Gilles
            Oct 31 '18 at 8:46











          • Considering that this is about Bash you could use an actual array to deal with escaping issues: cmd=(f /*line break*/ "arg1" /*line break*/ "arg2" ... ); x="$("${cmd[@]}")"

            – David Foerster
            Oct 31 '18 at 9:20













          • He said the arguments are positional. Whitespace would break it anyways.

            – ewatt
            Oct 31 '18 at 13:19



















          • This doesn't work if the arguments contain whitespace or characters that the shell interprets as wildcards. You can't store a list of arguments in a string variable.

            – Gilles
            Oct 31 '18 at 8:46











          • Considering that this is about Bash you could use an actual array to deal with escaping issues: cmd=(f /*line break*/ "arg1" /*line break*/ "arg2" ... ); x="$("${cmd[@]}")"

            – David Foerster
            Oct 31 '18 at 9:20













          • He said the arguments are positional. Whitespace would break it anyways.

            – ewatt
            Oct 31 '18 at 13:19

















          This doesn't work if the arguments contain whitespace or characters that the shell interprets as wildcards. You can't store a list of arguments in a string variable.

          – Gilles
          Oct 31 '18 at 8:46





          This doesn't work if the arguments contain whitespace or characters that the shell interprets as wildcards. You can't store a list of arguments in a string variable.

          – Gilles
          Oct 31 '18 at 8:46













          Considering that this is about Bash you could use an actual array to deal with escaping issues: cmd=(f /*line break*/ "arg1" /*line break*/ "arg2" ... ); x="$("${cmd[@]}")"

          – David Foerster
          Oct 31 '18 at 9:20







          Considering that this is about Bash you could use an actual array to deal with escaping issues: cmd=(f /*line break*/ "arg1" /*line break*/ "arg2" ... ); x="$("${cmd[@]}")"

          – David Foerster
          Oct 31 '18 at 9:20















          He said the arguments are positional. Whitespace would break it anyways.

          – ewatt
          Oct 31 '18 at 13:19





          He said the arguments are positional. Whitespace would break it anyways.

          – ewatt
          Oct 31 '18 at 13:19


















          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%2f478804%2fassign-variable-using-multiple-lines%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

          How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

          is 'sed' thread safe

          How to make a Squid Proxy server?