preserving backslash-newlines in heredocs run in a subshellsetting variables inside subshell when using...

How do people drown while wearing a life jacket?

Can I enter a rental property without giving notice if I'm afraid a tenant may be hurt?

what can you do with Format View

Can attackers change the public key of certificate during the SSL handshake

Broken bottom bracket?

How do the surviving Asgardians get to Earth?

How to call made-up data?

A Checkmate of Dubious Legality

Are the related objects in an SOQL query shared?

Does a humanoid possessed by a ghost register as undead to a paladin's Divine Sense?

Piece de Resistance - Introduction & Ace and A's

What are the limitations of the Hendersson-Hasselbalch equation?

Why did the US Airways Flight 1549 passengers stay on the wings?

Properties: Left of the colon

How to check a file was encrypted (really & correctly)

What is the reason behind water not falling from a bucket at the top of loop?

Custom Metadata SOQL WHERE clause not working

Ancients don't give a full level?

Awk to get all my regular users in shadow

Is an "are" omitted in this sentence

Is there a way to improve my grade after graduation?

Vectorised way to calculate mean of left and right neighbours in a vector

How to design an effective polearm-bow hybrid?

Why should I "believe in" weak solutions to PDEs?



preserving backslash-newlines in heredocs run in a subshell


setting variables inside subshell when using <<How to cat two heredocs?Standard subshell vs command-substitution subshellHow to run subshell commands over SSH?Why are heredocs “dependent” on file descriptors?How can you concatenate (or just combine) heredocuments (heredocs)?Why is there a bash keyword before some heredocs?Why must I use tabs instead of spaces in heredocs?Which shells (if any) avoid reading heredocs into memory all at once?Preserve backslash in $1






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















So long as terminal backslashes are not present, the newlines of a heredoc are preserved after capturing the output of a heredoc in a variable:



var=$(cat <<-EOF
a
b
c
EOF
)
echo "$var"

a
b
c


Newlines are lost, however, when trying to store backslash-newlines:



var=$(cat <<-EOF
a \
b \
c
EOF
)

echo "$var"
a b c


Adding additional backslashes for further escaping does not help.



Of course, when not run in a subshell, a heredoc can be used to generate lines that end in backslash-newline:



$ cat <<-EOF
a \
b \
c
EOF
a
b
c


To circumvent this problem, one can add a newline delimiter to generate the desired output by a transformation of the variable:



var=$(cat <<-EOF
a \@
b \@
c
EOF
)

tr -d '@' <<<"$var"

a
b
c


Is there a more straightforward way to preserve backslash-newlines from heredocs run in subshells?










share|improve this question

































    0















    So long as terminal backslashes are not present, the newlines of a heredoc are preserved after capturing the output of a heredoc in a variable:



    var=$(cat <<-EOF
    a
    b
    c
    EOF
    )
    echo "$var"

    a
    b
    c


    Newlines are lost, however, when trying to store backslash-newlines:



    var=$(cat <<-EOF
    a \
    b \
    c
    EOF
    )

    echo "$var"
    a b c


    Adding additional backslashes for further escaping does not help.



    Of course, when not run in a subshell, a heredoc can be used to generate lines that end in backslash-newline:



    $ cat <<-EOF
    a \
    b \
    c
    EOF
    a
    b
    c


    To circumvent this problem, one can add a newline delimiter to generate the desired output by a transformation of the variable:



    var=$(cat <<-EOF
    a \@
    b \@
    c
    EOF
    )

    tr -d '@' <<<"$var"

    a
    b
    c


    Is there a more straightforward way to preserve backslash-newlines from heredocs run in subshells?










    share|improve this question





























      0












      0








      0








      So long as terminal backslashes are not present, the newlines of a heredoc are preserved after capturing the output of a heredoc in a variable:



      var=$(cat <<-EOF
      a
      b
      c
      EOF
      )
      echo "$var"

      a
      b
      c


      Newlines are lost, however, when trying to store backslash-newlines:



      var=$(cat <<-EOF
      a \
      b \
      c
      EOF
      )

      echo "$var"
      a b c


      Adding additional backslashes for further escaping does not help.



      Of course, when not run in a subshell, a heredoc can be used to generate lines that end in backslash-newline:



      $ cat <<-EOF
      a \
      b \
      c
      EOF
      a
      b
      c


      To circumvent this problem, one can add a newline delimiter to generate the desired output by a transformation of the variable:



      var=$(cat <<-EOF
      a \@
      b \@
      c
      EOF
      )

      tr -d '@' <<<"$var"

      a
      b
      c


      Is there a more straightforward way to preserve backslash-newlines from heredocs run in subshells?










      share|improve this question
















      So long as terminal backslashes are not present, the newlines of a heredoc are preserved after capturing the output of a heredoc in a variable:



      var=$(cat <<-EOF
      a
      b
      c
      EOF
      )
      echo "$var"

      a
      b
      c


      Newlines are lost, however, when trying to store backslash-newlines:



      var=$(cat <<-EOF
      a \
      b \
      c
      EOF
      )

      echo "$var"
      a b c


      Adding additional backslashes for further escaping does not help.



      Of course, when not run in a subshell, a heredoc can be used to generate lines that end in backslash-newline:



      $ cat <<-EOF
      a \
      b \
      c
      EOF
      a
      b
      c


      To circumvent this problem, one can add a newline delimiter to generate the desired output by a transformation of the variable:



      var=$(cat <<-EOF
      a \@
      b \@
      c
      EOF
      )

      tr -d '@' <<<"$var"

      a
      b
      c


      Is there a more straightforward way to preserve backslash-newlines from heredocs run in subshells?







      bash here-document






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 3 hours ago







      user001

















      asked 3 hours ago









      user001user001

      1,7203 gold badges21 silver badges41 bronze badges




      1,7203 gold badges21 silver badges41 bronze badges

























          0






          active

          oldest

          votes














          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%2f534055%2fpreserving-backslash-newlines-in-heredocs-run-in-a-subshell%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f534055%2fpreserving-backslash-newlines-in-heredocs-run-in-a-subshell%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

          Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

          Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

          Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...