How can I remove a newline from redirected output

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












1















Can I remove a newline if I redirect output like this?



echo "a" >> file


I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:



a a a a a a a a a a


but it looks like this:



a
a
a
a
[etc.]









share|improve this question



















  • 1





    Use echo -n or printf.

    – DisplayName
    Jan 2 at 19:35












  • I should have looked at man echo in the first place. Thanks a lot!

    – KKor
    Jan 2 at 19:38







  • 3





    @KKor don't rely on man echo - your shell may have a builtin echo command that behaves differently. See for example Why is printf better than echo?

    – steeldriver
    Jan 2 at 19:47















1















Can I remove a newline if I redirect output like this?



echo "a" >> file


I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:



a a a a a a a a a a


but it looks like this:



a
a
a
a
[etc.]









share|improve this question



















  • 1





    Use echo -n or printf.

    – DisplayName
    Jan 2 at 19:35












  • I should have looked at man echo in the first place. Thanks a lot!

    – KKor
    Jan 2 at 19:38







  • 3





    @KKor don't rely on man echo - your shell may have a builtin echo command that behaves differently. See for example Why is printf better than echo?

    – steeldriver
    Jan 2 at 19:47













1












1








1








Can I remove a newline if I redirect output like this?



echo "a" >> file


I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:



a a a a a a a a a a


but it looks like this:



a
a
a
a
[etc.]









share|improve this question
















Can I remove a newline if I redirect output like this?



echo "a" >> file


I have something similar in my script which contains a for loop that redirects every character (except numbers) to a file.
But my goal is to have this output in one line. Let's say I redirect the "a" ten times so it should look like this:



a a a a a a a a a a


but it looks like this:



a
a
a
a
[etc.]






io-redirection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 19:54









DopeGhoti

43.9k55382




43.9k55382










asked Jan 2 at 19:33









KKorKKor

154




154







  • 1





    Use echo -n or printf.

    – DisplayName
    Jan 2 at 19:35












  • I should have looked at man echo in the first place. Thanks a lot!

    – KKor
    Jan 2 at 19:38







  • 3





    @KKor don't rely on man echo - your shell may have a builtin echo command that behaves differently. See for example Why is printf better than echo?

    – steeldriver
    Jan 2 at 19:47












  • 1





    Use echo -n or printf.

    – DisplayName
    Jan 2 at 19:35












  • I should have looked at man echo in the first place. Thanks a lot!

    – KKor
    Jan 2 at 19:38







  • 3





    @KKor don't rely on man echo - your shell may have a builtin echo command that behaves differently. See for example Why is printf better than echo?

    – steeldriver
    Jan 2 at 19:47







1




1





Use echo -n or printf.

– DisplayName
Jan 2 at 19:35






Use echo -n or printf.

– DisplayName
Jan 2 at 19:35














I should have looked at man echo in the first place. Thanks a lot!

– KKor
Jan 2 at 19:38






I should have looked at man echo in the first place. Thanks a lot!

– KKor
Jan 2 at 19:38





3




3





@KKor don't rely on man echo - your shell may have a builtin echo command that behaves differently. See for example Why is printf better than echo?

– steeldriver
Jan 2 at 19:47





@KKor don't rely on man echo - your shell may have a builtin echo command that behaves differently. See for example Why is printf better than echo?

– steeldriver
Jan 2 at 19:47










3 Answers
3






active

oldest

votes


















1














If you're specifically asking about echo, you can use -n to suppress the newline:



$ echo -n "test" >> outputfile


If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr:



$ do_stuff | tr -d "n" >> outputfile


If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo altogether in favor of printf:



$ printf "test" >> outputfile


printf is superior to echo in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).






share|improve this answer






























    1














    I don't think you'd want to delete the newlines, but replace them with spaces:



    for ((i=0; i<10; ++i)); do
    echo a
    done | tr 'n' ' ' >file


    This simply post-processes your echo output and saves the data into file (truncating it first, if it already exists).



    Or do something more fancy (in bash),



    for ((i=0; i<10; ++i)); do
    array+=( "a" )
    done

    printf '%sn' "$array[*]" >file


    This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS).



    Or, with /bin/sh:



    i=0
    set --
    while [ "$i" -lt 10 ]; do
    set -- "$@" "a"
    i=$(( i + 1 ))
    done

    printf '%sn' "$*" >file





    share|improve this answer






























      0














      for i in `seq 1 10`; do
      printf "a " >> file
      done


      should do the trick.






      share|improve this answer




















      • 1





        or for ... do ... done >> file to avoid reopening file multiple times

        – Jeff Schaller
        Jan 2 at 20:30










      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%2f492071%2fhow-can-i-remove-a-newline-from-redirected-output%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














      If you're specifically asking about echo, you can use -n to suppress the newline:



      $ echo -n "test" >> outputfile


      If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr:



      $ do_stuff | tr -d "n" >> outputfile


      If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo altogether in favor of printf:



      $ printf "test" >> outputfile


      printf is superior to echo in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).






      share|improve this answer



























        1














        If you're specifically asking about echo, you can use -n to suppress the newline:



        $ echo -n "test" >> outputfile


        If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr:



        $ do_stuff | tr -d "n" >> outputfile


        If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo altogether in favor of printf:



        $ printf "test" >> outputfile


        printf is superior to echo in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).






        share|improve this answer

























          1












          1








          1







          If you're specifically asking about echo, you can use -n to suppress the newline:



          $ echo -n "test" >> outputfile


          If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr:



          $ do_stuff | tr -d "n" >> outputfile


          If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo altogether in favor of printf:



          $ printf "test" >> outputfile


          printf is superior to echo in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).






          share|improve this answer













          If you're specifically asking about echo, you can use -n to suppress the newline:



          $ echo -n "test" >> outputfile


          If you're asking more generally how to suppress newlines in piped or redirected data, this is a cat with many skins. One easy way is with tr:



          $ do_stuff | tr -d "n" >> outputfile


          If you're just getting started with scripting and outputting data, it might be best to get out of the habit of using echo altogether in favor of printf:



          $ printf "test" >> outputfile


          printf is superior to echo in many ways and for many reasons, not least of which is that it only prints what you explicitly tell it to (and, arguably more importantly, in the format in which you want it).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 2 at 19:37









          DopeGhotiDopeGhoti

          43.9k55382




          43.9k55382























              1














              I don't think you'd want to delete the newlines, but replace them with spaces:



              for ((i=0; i<10; ++i)); do
              echo a
              done | tr 'n' ' ' >file


              This simply post-processes your echo output and saves the data into file (truncating it first, if it already exists).



              Or do something more fancy (in bash),



              for ((i=0; i<10; ++i)); do
              array+=( "a" )
              done

              printf '%sn' "$array[*]" >file


              This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS).



              Or, with /bin/sh:



              i=0
              set --
              while [ "$i" -lt 10 ]; do
              set -- "$@" "a"
              i=$(( i + 1 ))
              done

              printf '%sn' "$*" >file





              share|improve this answer



























                1














                I don't think you'd want to delete the newlines, but replace them with spaces:



                for ((i=0; i<10; ++i)); do
                echo a
                done | tr 'n' ' ' >file


                This simply post-processes your echo output and saves the data into file (truncating it first, if it already exists).



                Or do something more fancy (in bash),



                for ((i=0; i<10; ++i)); do
                array+=( "a" )
                done

                printf '%sn' "$array[*]" >file


                This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS).



                Or, with /bin/sh:



                i=0
                set --
                while [ "$i" -lt 10 ]; do
                set -- "$@" "a"
                i=$(( i + 1 ))
                done

                printf '%sn' "$*" >file





                share|improve this answer

























                  1












                  1








                  1







                  I don't think you'd want to delete the newlines, but replace them with spaces:



                  for ((i=0; i<10; ++i)); do
                  echo a
                  done | tr 'n' ' ' >file


                  This simply post-processes your echo output and saves the data into file (truncating it first, if it already exists).



                  Or do something more fancy (in bash),



                  for ((i=0; i<10; ++i)); do
                  array+=( "a" )
                  done

                  printf '%sn' "$array[*]" >file


                  This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS).



                  Or, with /bin/sh:



                  i=0
                  set --
                  while [ "$i" -lt 10 ]; do
                  set -- "$@" "a"
                  i=$(( i + 1 ))
                  done

                  printf '%sn' "$*" >file





                  share|improve this answer













                  I don't think you'd want to delete the newlines, but replace them with spaces:



                  for ((i=0; i<10; ++i)); do
                  echo a
                  done | tr 'n' ' ' >file


                  This simply post-processes your echo output and saves the data into file (truncating it first, if it already exists).



                  Or do something more fancy (in bash),



                  for ((i=0; i<10; ++i)); do
                  array+=( "a" )
                  done

                  printf '%sn' "$array[*]" >file


                  This has the advantage of terminating the line properly with a final newline, while all the elements of the array are delimited by space (the first character of $IFS).



                  Or, with /bin/sh:



                  i=0
                  set --
                  while [ "$i" -lt 10 ]; do
                  set -- "$@" "a"
                  i=$(( i + 1 ))
                  done

                  printf '%sn' "$*" >file






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 20:22









                  KusalanandaKusalananda

                  124k16234385




                  124k16234385





















                      0














                      for i in `seq 1 10`; do
                      printf "a " >> file
                      done


                      should do the trick.






                      share|improve this answer




















                      • 1





                        or for ... do ... done >> file to avoid reopening file multiple times

                        – Jeff Schaller
                        Jan 2 at 20:30















                      0














                      for i in `seq 1 10`; do
                      printf "a " >> file
                      done


                      should do the trick.






                      share|improve this answer




















                      • 1





                        or for ... do ... done >> file to avoid reopening file multiple times

                        – Jeff Schaller
                        Jan 2 at 20:30













                      0












                      0








                      0







                      for i in `seq 1 10`; do
                      printf "a " >> file
                      done


                      should do the trick.






                      share|improve this answer















                      for i in `seq 1 10`; do
                      printf "a " >> file
                      done


                      should do the trick.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 2 at 20:30









                      Jeff Schaller

                      39.3k1054125




                      39.3k1054125










                      answered Jan 2 at 19:38









                      onur güngöronur güngör

                      764513




                      764513







                      • 1





                        or for ... do ... done >> file to avoid reopening file multiple times

                        – Jeff Schaller
                        Jan 2 at 20:30












                      • 1





                        or for ... do ... done >> file to avoid reopening file multiple times

                        – Jeff Schaller
                        Jan 2 at 20:30







                      1




                      1





                      or for ... do ... done >> file to avoid reopening file multiple times

                      – Jeff Schaller
                      Jan 2 at 20:30





                      or for ... do ... done >> file to avoid reopening file multiple times

                      – Jeff Schaller
                      Jan 2 at 20:30

















                      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%2f492071%2fhow-can-i-remove-a-newline-from-redirected-output%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 check contact read email or not when send email to Individual?

                      Displaying single band from multi-band raster using QGIS

                      How many registers does an x86_64 CPU actually have?