How to read input lines with newline characters from command line?shell: read: differentiate between EOF and...

How could an armless race establish civilization?

Different budgets within roommate group

How receiver knows the exact frequency in the channel to "listen to"?

How is this practical and very old scene shot?

How do I ensure my employees don't abuse my flexible work hours policy?

How Do I Know When I am in Private Mode?

Security Patch SUPEE-11155 - Possible issues?

Can a nowhere continuous function have a connected graph?

Why wasn't EBCDIC designed with contiguous alphanumeric characters?

What will happen if I checked in for another room in the same hotel, but not for the booked one?

Divergent Series & Continued Fraction (from Gauss' Mathematical Diary)

"save as image" no confirmation when overwriting

What is "oversubscription" in Networking?

If two black hole event horizons overlap (touch) can they ever separate again?

for xml path('') output

13th chords on guitar

Comment traduire « That screams X »

Company threatening to call my current job after I declined their offer

Closest Proximity of Oceans to Freshwater Springs

Does Latin have any neuter words for humans?

Two palindromes are not enough

I hit a pipe with a mower and now it won't turn

Is there reliable evidence that depleted uranium from the 1999 NATO bombing is causing cancer in Serbia?

Using “ser” without "un/una"?



How to read input lines with newline characters from command line?


shell: read: differentiate between EOF and newlinebash: read: how to capture 'n' (newline) character?What does the -p option do in the read command?Read arguments separated by newlineBash newline doesn't printprocessing command output line by line, without mixing standard inputHow to combine Bash's read with HERE-document when shopt -os errexit is in place?How is a trace of commands different from shell input lines?How to signal end-of-input to “read -N”?How to use linebreak in read -p command while using variables in prompt stringHow to make a prefill line with read command?how to check if the first line of file contain a specific string?How to bind a key in bash to a function that modifies the current command lineIs there a character as `delim` of `read`, so that `read` reads the entire of a file at once?






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







0















I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:



#!/bin/bash
read -e -p "Multiline input=" variable;
printf "'variable=%s'" "${variable}";



  1. Typing 'multinline' on Multiline input= makes printf output 'variable=multinline'

  2. Typing 'multi\nline' on Multiline input= makes printf output 'variable=multinline'


How printf can print the new line I read by read -p, i.e., output



multi
line



Instead of multinline or multinline?



Related questions:




  1. What does the -p option do in the read command?

  2. bash: read: how to capture 'n' (newline) character?

  3. shell: read: differentiate between EOF and newline

  4. https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command

  5. Read arguments separated by newline

  6. https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting










share|improve this question





























    0















    I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:



    #!/bin/bash
    read -e -p "Multiline input=" variable;
    printf "'variable=%s'" "${variable}";



    1. Typing 'multinline' on Multiline input= makes printf output 'variable=multinline'

    2. Typing 'multi\nline' on Multiline input= makes printf output 'variable=multinline'


    How printf can print the new line I read by read -p, i.e., output



    multi
    line



    Instead of multinline or multinline?



    Related questions:




    1. What does the -p option do in the read command?

    2. bash: read: how to capture 'n' (newline) character?

    3. shell: read: differentiate between EOF and newline

    4. https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command

    5. Read arguments separated by newline

    6. https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting










    share|improve this question

























      0












      0








      0








      I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:



      #!/bin/bash
      read -e -p "Multiline input=" variable;
      printf "'variable=%s'" "${variable}";



      1. Typing 'multinline' on Multiline input= makes printf output 'variable=multinline'

      2. Typing 'multi\nline' on Multiline input= makes printf output 'variable=multinline'


      How printf can print the new line I read by read -p, i.e., output



      multi
      line



      Instead of multinline or multinline?



      Related questions:




      1. What does the -p option do in the read command?

      2. bash: read: how to capture 'n' (newline) character?

      3. shell: read: differentiate between EOF and newline

      4. https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command

      5. Read arguments separated by newline

      6. https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting










      share|improve this question














      I found this to get user input from the command line. But it is failing into recognizing the new line characters I put into the input. Doing:



      #!/bin/bash
      read -e -p "Multiline input=" variable;
      printf "'variable=%s'" "${variable}";



      1. Typing 'multinline' on Multiline input= makes printf output 'variable=multinline'

      2. Typing 'multi\nline' on Multiline input= makes printf output 'variable=multinline'


      How printf can print the new line I read by read -p, i.e., output



      multi
      line



      Instead of multinline or multinline?



      Related questions:




      1. What does the -p option do in the read command?

      2. bash: read: how to capture 'n' (newline) character?

      3. shell: read: differentiate between EOF and newline

      4. https://stackoverflow.com/questions/4296108/how-do-i-add-a-line-break-for-read-command

      5. Read arguments separated by newline

      6. https://stackoverflow.com/questions/43190306/how-to-add-new-line-after-user-input-in-shell-scripting







      bash shell posix input read






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 48 mins ago









      useruser

      1561 silver badge11 bronze badges




      1561 silver badge11 bronze badges






















          1 Answer
          1






          active

          oldest

          votes


















          0














          If typing in n (as in the two characters and n) is acceptable, then you can use printf to interpret it:



          #!/bin/bash
          IFS= read -rep "Multiline input=" variable;
          printf -v variable "%b" "$variable"
          printf "'variable=%s'n" "${variable}";


          For example:



          ~ ./foo.sh
          Multiline input=foonbar
          'variable=foo
          bar'


          From the bash manual:




          The backslash character ‘’ may be used to remove any special meaning
          for the next character read and for line continuation.




          The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.






          share|improve this answer
























          • if already using bash, then instead of printf %b ... the OP could use just variable=${variable//\n/$'n'} (and unlike printf -v, this will also work in ksh and mksh).

            – mosvy
            3 mins ago














          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%2f527170%2fhow-to-read-input-lines-with-newline-characters-from-command-line%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          If typing in n (as in the two characters and n) is acceptable, then you can use printf to interpret it:



          #!/bin/bash
          IFS= read -rep "Multiline input=" variable;
          printf -v variable "%b" "$variable"
          printf "'variable=%s'n" "${variable}";


          For example:



          ~ ./foo.sh
          Multiline input=foonbar
          'variable=foo
          bar'


          From the bash manual:




          The backslash character ‘’ may be used to remove any special meaning
          for the next character read and for line continuation.




          The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.






          share|improve this answer
























          • if already using bash, then instead of printf %b ... the OP could use just variable=${variable//\n/$'n'} (and unlike printf -v, this will also work in ksh and mksh).

            – mosvy
            3 mins ago
















          0














          If typing in n (as in the two characters and n) is acceptable, then you can use printf to interpret it:



          #!/bin/bash
          IFS= read -rep "Multiline input=" variable;
          printf -v variable "%b" "$variable"
          printf "'variable=%s'n" "${variable}";


          For example:



          ~ ./foo.sh
          Multiline input=foonbar
          'variable=foo
          bar'


          From the bash manual:




          The backslash character ‘’ may be used to remove any special meaning
          for the next character read and for line continuation.




          The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.






          share|improve this answer
























          • if already using bash, then instead of printf %b ... the OP could use just variable=${variable//\n/$'n'} (and unlike printf -v, this will also work in ksh and mksh).

            – mosvy
            3 mins ago














          0












          0








          0







          If typing in n (as in the two characters and n) is acceptable, then you can use printf to interpret it:



          #!/bin/bash
          IFS= read -rep "Multiline input=" variable;
          printf -v variable "%b" "$variable"
          printf "'variable=%s'n" "${variable}";


          For example:



          ~ ./foo.sh
          Multiline input=foonbar
          'variable=foo
          bar'


          From the bash manual:




          The backslash character ‘’ may be used to remove any special meaning
          for the next character read and for line continuation.




          The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.






          share|improve this answer













          If typing in n (as in the two characters and n) is acceptable, then you can use printf to interpret it:



          #!/bin/bash
          IFS= read -rep "Multiline input=" variable;
          printf -v variable "%b" "$variable"
          printf "'variable=%s'n" "${variable}";


          For example:



          ~ ./foo.sh
          Multiline input=foonbar
          'variable=foo
          bar'


          From the bash manual:




          The backslash character ‘’ may be used to remove any special meaning
          for the next character read and for line continuation.




          The "line continuation" bit seems to imply you can't escape newlines unless you use a different character as the line delimiter.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 27 mins ago









          murumuru

          40.5k5 gold badges98 silver badges172 bronze badges




          40.5k5 gold badges98 silver badges172 bronze badges













          • if already using bash, then instead of printf %b ... the OP could use just variable=${variable//\n/$'n'} (and unlike printf -v, this will also work in ksh and mksh).

            – mosvy
            3 mins ago



















          • if already using bash, then instead of printf %b ... the OP could use just variable=${variable//\n/$'n'} (and unlike printf -v, this will also work in ksh and mksh).

            – mosvy
            3 mins ago

















          if already using bash, then instead of printf %b ... the OP could use just variable=${variable//\n/$'n'} (and unlike printf -v, this will also work in ksh and mksh).

          – mosvy
          3 mins ago





          if already using bash, then instead of printf %b ... the OP could use just variable=${variable//\n/$'n'} (and unlike printf -v, this will also work in ksh and mksh).

          – mosvy
          3 mins ago


















          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%2f527170%2fhow-to-read-input-lines-with-newline-characters-from-command-line%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...