append text with echo without new line

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











up vote
7
down vote

favorite












I want to append text to file like echo "abc" >>file.txt.



But this add abc after new line



How can I add abc in the end of file with echo without new line?







share|improve this question


















  • 2




    The file already has a newline, you are just adding after it. So you will have to replace the newline character from the last line, with “abc”.
    – ctrl-alt-delor
    Dec 24 '17 at 19:16










  • Welcome to StackExchange! Your question is good; it would have been better if you specified examples of the content of your file (before addition, what you get after addition, what you wanted instead). I say this because one of the answers is how to add abc without a final newline, which (after reading your question attentively) does not seem to be what you want.
    – Law29
    Dec 25 '17 at 4:06














up vote
7
down vote

favorite












I want to append text to file like echo "abc" >>file.txt.



But this add abc after new line



How can I add abc in the end of file with echo without new line?







share|improve this question


















  • 2




    The file already has a newline, you are just adding after it. So you will have to replace the newline character from the last line, with “abc”.
    – ctrl-alt-delor
    Dec 24 '17 at 19:16










  • Welcome to StackExchange! Your question is good; it would have been better if you specified examples of the content of your file (before addition, what you get after addition, what you wanted instead). I say this because one of the answers is how to add abc without a final newline, which (after reading your question attentively) does not seem to be what you want.
    – Law29
    Dec 25 '17 at 4:06












up vote
7
down vote

favorite









up vote
7
down vote

favorite











I want to append text to file like echo "abc" >>file.txt.



But this add abc after new line



How can I add abc in the end of file with echo without new line?







share|improve this question














I want to append text to file like echo "abc" >>file.txt.



But this add abc after new line



How can I add abc in the end of file with echo without new line?









share|improve this question













share|improve this question




share|improve this question








edited Dec 24 '17 at 18:03









αԋɱҽԃ αмєяιcαη

407418




407418










asked Dec 24 '17 at 17:16









choijj

3612




3612







  • 2




    The file already has a newline, you are just adding after it. So you will have to replace the newline character from the last line, with “abc”.
    – ctrl-alt-delor
    Dec 24 '17 at 19:16










  • Welcome to StackExchange! Your question is good; it would have been better if you specified examples of the content of your file (before addition, what you get after addition, what you wanted instead). I say this because one of the answers is how to add abc without a final newline, which (after reading your question attentively) does not seem to be what you want.
    – Law29
    Dec 25 '17 at 4:06












  • 2




    The file already has a newline, you are just adding after it. So you will have to replace the newline character from the last line, with “abc”.
    – ctrl-alt-delor
    Dec 24 '17 at 19:16










  • Welcome to StackExchange! Your question is good; it would have been better if you specified examples of the content of your file (before addition, what you get after addition, what you wanted instead). I say this because one of the answers is how to add abc without a final newline, which (after reading your question attentively) does not seem to be what you want.
    – Law29
    Dec 25 '17 at 4:06







2




2




The file already has a newline, you are just adding after it. So you will have to replace the newline character from the last line, with “abc”.
– ctrl-alt-delor
Dec 24 '17 at 19:16




The file already has a newline, you are just adding after it. So you will have to replace the newline character from the last line, with “abc”.
– ctrl-alt-delor
Dec 24 '17 at 19:16












Welcome to StackExchange! Your question is good; it would have been better if you specified examples of the content of your file (before addition, what you get after addition, what you wanted instead). I say this because one of the answers is how to add abc without a final newline, which (after reading your question attentively) does not seem to be what you want.
– Law29
Dec 25 '17 at 4:06




Welcome to StackExchange! Your question is good; it would have been better if you specified examples of the content of your file (before addition, what you get after addition, what you wanted instead). I say this because one of the answers is how to add abc without a final newline, which (after reading your question attentively) does not seem to be what you want.
– Law29
Dec 25 '17 at 4:06










6 Answers
6






active

oldest

votes

















up vote
14
down vote













echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.



Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline.1 Therefore any non-empty text file ends with a newline character.



If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:



sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt


In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.



Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to



sed -i '$ s/$/abc/' file.txt


⁰ That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.
1 See the definitions of Text File, Line, and Newline Character in the "Definitions" section of the Base Definitions chapter of IEEE 1003.1-2008:2016.






share|improve this answer


















  • 1




    Is your second paragraph meant to imply that a file which doesn't end with a newline is not a text file? E.g. if I take an existing ASCII text file which ends with a new line and append a single byte 0x41 (ASCII 'A'), it's technically no longer a text file? If so, I'd suggest emphasizing that point since it's kind of an unintuitive definition; if not, a slight change in wording might help avoid the confusion.
    – David Z
    Dec 25 '17 at 11:07






  • 2




    @DavidZ: That is the standard definition of a text file in Unixland. IIRC it's even in POSIX somewhere.
    – Kevin
    Dec 25 '17 at 12:56










  • @Kevin Interesting, I'd never heard that before. Well, even if it is standard, I do think it's kind of unintuitive.
    – David Z
    Dec 25 '17 at 23:13

















up vote
9
down vote













I don't think it's possible with echo command, use the following sed approach instead:



sed -i '$ s/$/abc/' file.txt



  • -i - modify the file inplace


  • $ - indicate the last record/line


  • s/$/abc/ - substitute the end of the line $ with substring abc (for the last record)





share|improve this answer


















  • 2




    Note that "in place" doesn't really mean in place. It means "write the edited content to a temporary named file alongside the existing file and then replace it". You can prove this by looking at the inodes with date >file; ls -i file; sed -i 's/201/ZZZ/' file; ls -i file
    – roaima
    Dec 25 '17 at 0:47










  • @roaima, I'm aware about sed changing inode number.
    – RomanPerekhrest
    Dec 25 '17 at 6:38






  • 2




    I thought you would have been, but i was concerned that with your emphasis of inplace the OP night think it could be used to avoid double disk space use, eg with a large file.
    – roaima
    Dec 25 '17 at 8:51











  • @roaima, night think -> might think ...
    – RomanPerekhrest
    Dec 25 '17 at 8:56

















up vote
8
down vote













If you have the truncate command and your text file has NL as its last character you can remove it and then append your text like this:



truncate --size -1 file.txt
echo "abc" >>file.txt


(Note that truncate cares nothing about file content, and in this example simply reduces the file size by one byte. If your last character is not a single byte, ie it's a multi-byte "wide" character then you will introduce corruption.)






share|improve this answer





























    up vote
    5
    down vote













    Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g.



    echo -n "some text here" >> file.txt


    However, some UNIX systems do not provide this option; if that is the case you can use printf, e.g.



    printf %s "some text here" >> file.txt


    (the initial %s argument being to guard against the additional text having % formatting characters in it)



    From man echo (on macOS High Sierra):




    -n




    Do not print the trailing newline character. This may also be achieved by appending 'c' to the end of the string, as is done by iBCS2 compatible systems. Note that this option as well as the effect of 'c' are implementation-defined in IEEE Std 1003.1-2001 ("POSIX.1") as amended by Cor. 1-2002. Applications aiming for maximum portability are strongly encouraged to use printf(1) to suppress the newline character.








    share|improve this answer






















    • It quite obviously does end with a newline. echo -n would put no newline at the end of abc, but abc would still be preceded by a newline, which is what the user wants to avoid.
      – Kusalananda
      Jul 14 at 8:53










    • @Kusalananda My answer was more using the assumption that the OP would try to change their process such that the spurious n didn't show up in the first place. More options are better, especially when they don't involve having to rewrite the entire file every time a change happens (which can get rather slow and run in polynomial time if it's done repeatedly).
      – fluffy
      Jul 16 at 1:02

















    up vote
    4
    down vote













    What you want is to add it at the end of the last line, so just before the delimiter of that last line, so just before the last character of the file.



    With ksh93, you can do:



    echo abc 1<> file >#((EOF - 1))


    Where 1<> is the standard operator to open a file in read+write mode (and more importantly without truncation) on stdout and >#((...)) is a ksh93-specific seeking operator (here to seek to before the last character). Note that echo writes abc<newline> where the a overwrites the newline that was there and echo adds its own newline.



    The zsh equivalent:



    zmodload zsh/system
    sysseek -w end -u 1 -1; echo abc 1<> file





    share|improve this answer



























      up vote
      -1
      down vote













      Probably a UUOC but you could also do:



      echo "$(cat file.txt)abc" >file.txt


      As Gilles points out this command is limited:




      This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.




      Additionally be wary of using cat on files you are not familiar with:



      • CVE-2003-0063

      • CVE-2008-2383

      • CVE-2010-2713

      • CVE-2003-0020

      • CVE-2012-3515

      • More

      Conclusion:



      Use sed






      share|improve this answer


















      • 2




        This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.
        – Gilles
        Dec 24 '17 at 17:49










      • Should I delete it? I definitely think Roman/your answer is the proper way to go but I know I personally like seeing alternatives, and OP did ask for echo :p
        – Jesse_b
        Dec 24 '17 at 17:51






      • 1




        Also this puts the entire file on the command line
        – n.caillou
        Dec 24 '17 at 19:34










      • @n.caillou huh? This will not print anything to STDOUT.
        – Jesse_b
        Dec 24 '17 at 20:54






      • 2




        Well, none of those vulnerabilities are really related to cat, but to issues with arbitrary escape sequences being sent to the terminal.
        – ilkkachu
        Dec 24 '17 at 21:02










      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: "",
      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%2f412835%2fappend-text-with-echo-without-new-line%23new-answer', 'question_page');

      );

      Post as a guest






























      6 Answers
      6






      active

      oldest

      votes








      6 Answers
      6






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      14
      down vote













      echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.



      Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline.1 Therefore any non-empty text file ends with a newline character.



      If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:



      sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt


      In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.



      Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to



      sed -i '$ s/$/abc/' file.txt


      ⁰ That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.
      1 See the definitions of Text File, Line, and Newline Character in the "Definitions" section of the Base Definitions chapter of IEEE 1003.1-2008:2016.






      share|improve this answer


















      • 1




        Is your second paragraph meant to imply that a file which doesn't end with a newline is not a text file? E.g. if I take an existing ASCII text file which ends with a new line and append a single byte 0x41 (ASCII 'A'), it's technically no longer a text file? If so, I'd suggest emphasizing that point since it's kind of an unintuitive definition; if not, a slight change in wording might help avoid the confusion.
        – David Z
        Dec 25 '17 at 11:07






      • 2




        @DavidZ: That is the standard definition of a text file in Unixland. IIRC it's even in POSIX somewhere.
        – Kevin
        Dec 25 '17 at 12:56










      • @Kevin Interesting, I'd never heard that before. Well, even if it is standard, I do think it's kind of unintuitive.
        – David Z
        Dec 25 '17 at 23:13














      up vote
      14
      down vote













      echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.



      Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline.1 Therefore any non-empty text file ends with a newline character.



      If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:



      sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt


      In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.



      Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to



      sed -i '$ s/$/abc/' file.txt


      ⁰ That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.
      1 See the definitions of Text File, Line, and Newline Character in the "Definitions" section of the Base Definitions chapter of IEEE 1003.1-2008:2016.






      share|improve this answer


















      • 1




        Is your second paragraph meant to imply that a file which doesn't end with a newline is not a text file? E.g. if I take an existing ASCII text file which ends with a new line and append a single byte 0x41 (ASCII 'A'), it's technically no longer a text file? If so, I'd suggest emphasizing that point since it's kind of an unintuitive definition; if not, a slight change in wording might help avoid the confusion.
        – David Z
        Dec 25 '17 at 11:07






      • 2




        @DavidZ: That is the standard definition of a text file in Unixland. IIRC it's even in POSIX somewhere.
        – Kevin
        Dec 25 '17 at 12:56










      • @Kevin Interesting, I'd never heard that before. Well, even if it is standard, I do think it's kind of unintuitive.
        – David Z
        Dec 25 '17 at 23:13












      up vote
      14
      down vote










      up vote
      14
      down vote









      echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.



      Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline.1 Therefore any non-empty text file ends with a newline character.



      If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:



      sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt


      In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.



      Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to



      sed -i '$ s/$/abc/' file.txt


      ⁰ That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.
      1 See the definitions of Text File, Line, and Newline Character in the "Definitions" section of the Base Definitions chapter of IEEE 1003.1-2008:2016.






      share|improve this answer














      echo "abc" >>file.txt puts a newline after abc, not before. If you end up with abc on its own line, that means that the newline before abc was already present in file.txt.



      Note that it is perfectly normal for a text file to end in a newline. On unix, a line consists of a sequence of characters other than null⁰ or newline followed by a newline.1 Therefore any non-empty text file ends with a newline character.



      If you want to add text to the last line of a file, then you can't do it with >>, because this always appends to the file, so it always writes after the last newline. Instead you need a tool that is capable of modifying an existing file. For example, you can use sed:



      sed '$ s/$/abc/' file.txt >file.txt.new && mv file.txt.new file.txt


      In the sed command, the first $ means “do the following command only on the last line”, the command s/REGEX/REPLACEMENT/ replaces REGEX by REPLACEMENT, and the regular expression $ matches at the end of the line.



      Linux's sed command has a built-in feature to automate this create-new-file-and-replace sequence, so you can shorten that to



      sed -i '$ s/$/abc/' file.txt


      ⁰ That's a null byte, which ASCII calls NUL and Unicode calls U+0000. Text processing programs may or may not cope with this character.
      1 See the definitions of Text File, Line, and Newline Character in the "Definitions" section of the Base Definitions chapter of IEEE 1003.1-2008:2016.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 25 '17 at 14:15









      JdeBP

      28.6k459134




      28.6k459134










      answered Dec 24 '17 at 17:36









      Gilles

      506k12010031529




      506k12010031529







      • 1




        Is your second paragraph meant to imply that a file which doesn't end with a newline is not a text file? E.g. if I take an existing ASCII text file which ends with a new line and append a single byte 0x41 (ASCII 'A'), it's technically no longer a text file? If so, I'd suggest emphasizing that point since it's kind of an unintuitive definition; if not, a slight change in wording might help avoid the confusion.
        – David Z
        Dec 25 '17 at 11:07






      • 2




        @DavidZ: That is the standard definition of a text file in Unixland. IIRC it's even in POSIX somewhere.
        – Kevin
        Dec 25 '17 at 12:56










      • @Kevin Interesting, I'd never heard that before. Well, even if it is standard, I do think it's kind of unintuitive.
        – David Z
        Dec 25 '17 at 23:13












      • 1




        Is your second paragraph meant to imply that a file which doesn't end with a newline is not a text file? E.g. if I take an existing ASCII text file which ends with a new line and append a single byte 0x41 (ASCII 'A'), it's technically no longer a text file? If so, I'd suggest emphasizing that point since it's kind of an unintuitive definition; if not, a slight change in wording might help avoid the confusion.
        – David Z
        Dec 25 '17 at 11:07






      • 2




        @DavidZ: That is the standard definition of a text file in Unixland. IIRC it's even in POSIX somewhere.
        – Kevin
        Dec 25 '17 at 12:56










      • @Kevin Interesting, I'd never heard that before. Well, even if it is standard, I do think it's kind of unintuitive.
        – David Z
        Dec 25 '17 at 23:13







      1




      1




      Is your second paragraph meant to imply that a file which doesn't end with a newline is not a text file? E.g. if I take an existing ASCII text file which ends with a new line and append a single byte 0x41 (ASCII 'A'), it's technically no longer a text file? If so, I'd suggest emphasizing that point since it's kind of an unintuitive definition; if not, a slight change in wording might help avoid the confusion.
      – David Z
      Dec 25 '17 at 11:07




      Is your second paragraph meant to imply that a file which doesn't end with a newline is not a text file? E.g. if I take an existing ASCII text file which ends with a new line and append a single byte 0x41 (ASCII 'A'), it's technically no longer a text file? If so, I'd suggest emphasizing that point since it's kind of an unintuitive definition; if not, a slight change in wording might help avoid the confusion.
      – David Z
      Dec 25 '17 at 11:07




      2




      2




      @DavidZ: That is the standard definition of a text file in Unixland. IIRC it's even in POSIX somewhere.
      – Kevin
      Dec 25 '17 at 12:56




      @DavidZ: That is the standard definition of a text file in Unixland. IIRC it's even in POSIX somewhere.
      – Kevin
      Dec 25 '17 at 12:56












      @Kevin Interesting, I'd never heard that before. Well, even if it is standard, I do think it's kind of unintuitive.
      – David Z
      Dec 25 '17 at 23:13




      @Kevin Interesting, I'd never heard that before. Well, even if it is standard, I do think it's kind of unintuitive.
      – David Z
      Dec 25 '17 at 23:13












      up vote
      9
      down vote













      I don't think it's possible with echo command, use the following sed approach instead:



      sed -i '$ s/$/abc/' file.txt



      • -i - modify the file inplace


      • $ - indicate the last record/line


      • s/$/abc/ - substitute the end of the line $ with substring abc (for the last record)





      share|improve this answer


















      • 2




        Note that "in place" doesn't really mean in place. It means "write the edited content to a temporary named file alongside the existing file and then replace it". You can prove this by looking at the inodes with date >file; ls -i file; sed -i 's/201/ZZZ/' file; ls -i file
        – roaima
        Dec 25 '17 at 0:47










      • @roaima, I'm aware about sed changing inode number.
        – RomanPerekhrest
        Dec 25 '17 at 6:38






      • 2




        I thought you would have been, but i was concerned that with your emphasis of inplace the OP night think it could be used to avoid double disk space use, eg with a large file.
        – roaima
        Dec 25 '17 at 8:51











      • @roaima, night think -> might think ...
        – RomanPerekhrest
        Dec 25 '17 at 8:56














      up vote
      9
      down vote













      I don't think it's possible with echo command, use the following sed approach instead:



      sed -i '$ s/$/abc/' file.txt



      • -i - modify the file inplace


      • $ - indicate the last record/line


      • s/$/abc/ - substitute the end of the line $ with substring abc (for the last record)





      share|improve this answer


















      • 2




        Note that "in place" doesn't really mean in place. It means "write the edited content to a temporary named file alongside the existing file and then replace it". You can prove this by looking at the inodes with date >file; ls -i file; sed -i 's/201/ZZZ/' file; ls -i file
        – roaima
        Dec 25 '17 at 0:47










      • @roaima, I'm aware about sed changing inode number.
        – RomanPerekhrest
        Dec 25 '17 at 6:38






      • 2




        I thought you would have been, but i was concerned that with your emphasis of inplace the OP night think it could be used to avoid double disk space use, eg with a large file.
        – roaima
        Dec 25 '17 at 8:51











      • @roaima, night think -> might think ...
        – RomanPerekhrest
        Dec 25 '17 at 8:56












      up vote
      9
      down vote










      up vote
      9
      down vote









      I don't think it's possible with echo command, use the following sed approach instead:



      sed -i '$ s/$/abc/' file.txt



      • -i - modify the file inplace


      • $ - indicate the last record/line


      • s/$/abc/ - substitute the end of the line $ with substring abc (for the last record)





      share|improve this answer














      I don't think it's possible with echo command, use the following sed approach instead:



      sed -i '$ s/$/abc/' file.txt



      • -i - modify the file inplace


      • $ - indicate the last record/line


      • s/$/abc/ - substitute the end of the line $ with substring abc (for the last record)






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 24 '17 at 19:54

























      answered Dec 24 '17 at 17:23









      RomanPerekhrest

      22.4k12145




      22.4k12145







      • 2




        Note that "in place" doesn't really mean in place. It means "write the edited content to a temporary named file alongside the existing file and then replace it". You can prove this by looking at the inodes with date >file; ls -i file; sed -i 's/201/ZZZ/' file; ls -i file
        – roaima
        Dec 25 '17 at 0:47










      • @roaima, I'm aware about sed changing inode number.
        – RomanPerekhrest
        Dec 25 '17 at 6:38






      • 2




        I thought you would have been, but i was concerned that with your emphasis of inplace the OP night think it could be used to avoid double disk space use, eg with a large file.
        – roaima
        Dec 25 '17 at 8:51











      • @roaima, night think -> might think ...
        – RomanPerekhrest
        Dec 25 '17 at 8:56












      • 2




        Note that "in place" doesn't really mean in place. It means "write the edited content to a temporary named file alongside the existing file and then replace it". You can prove this by looking at the inodes with date >file; ls -i file; sed -i 's/201/ZZZ/' file; ls -i file
        – roaima
        Dec 25 '17 at 0:47










      • @roaima, I'm aware about sed changing inode number.
        – RomanPerekhrest
        Dec 25 '17 at 6:38






      • 2




        I thought you would have been, but i was concerned that with your emphasis of inplace the OP night think it could be used to avoid double disk space use, eg with a large file.
        – roaima
        Dec 25 '17 at 8:51











      • @roaima, night think -> might think ...
        – RomanPerekhrest
        Dec 25 '17 at 8:56







      2




      2




      Note that "in place" doesn't really mean in place. It means "write the edited content to a temporary named file alongside the existing file and then replace it". You can prove this by looking at the inodes with date >file; ls -i file; sed -i 's/201/ZZZ/' file; ls -i file
      – roaima
      Dec 25 '17 at 0:47




      Note that "in place" doesn't really mean in place. It means "write the edited content to a temporary named file alongside the existing file and then replace it". You can prove this by looking at the inodes with date >file; ls -i file; sed -i 's/201/ZZZ/' file; ls -i file
      – roaima
      Dec 25 '17 at 0:47












      @roaima, I'm aware about sed changing inode number.
      – RomanPerekhrest
      Dec 25 '17 at 6:38




      @roaima, I'm aware about sed changing inode number.
      – RomanPerekhrest
      Dec 25 '17 at 6:38




      2




      2




      I thought you would have been, but i was concerned that with your emphasis of inplace the OP night think it could be used to avoid double disk space use, eg with a large file.
      – roaima
      Dec 25 '17 at 8:51





      I thought you would have been, but i was concerned that with your emphasis of inplace the OP night think it could be used to avoid double disk space use, eg with a large file.
      – roaima
      Dec 25 '17 at 8:51













      @roaima, night think -> might think ...
      – RomanPerekhrest
      Dec 25 '17 at 8:56




      @roaima, night think -> might think ...
      – RomanPerekhrest
      Dec 25 '17 at 8:56










      up vote
      8
      down vote













      If you have the truncate command and your text file has NL as its last character you can remove it and then append your text like this:



      truncate --size -1 file.txt
      echo "abc" >>file.txt


      (Note that truncate cares nothing about file content, and in this example simply reduces the file size by one byte. If your last character is not a single byte, ie it's a multi-byte "wide" character then you will introduce corruption.)






      share|improve this answer


























        up vote
        8
        down vote













        If you have the truncate command and your text file has NL as its last character you can remove it and then append your text like this:



        truncate --size -1 file.txt
        echo "abc" >>file.txt


        (Note that truncate cares nothing about file content, and in this example simply reduces the file size by one byte. If your last character is not a single byte, ie it's a multi-byte "wide" character then you will introduce corruption.)






        share|improve this answer
























          up vote
          8
          down vote










          up vote
          8
          down vote









          If you have the truncate command and your text file has NL as its last character you can remove it and then append your text like this:



          truncate --size -1 file.txt
          echo "abc" >>file.txt


          (Note that truncate cares nothing about file content, and in this example simply reduces the file size by one byte. If your last character is not a single byte, ie it's a multi-byte "wide" character then you will introduce corruption.)






          share|improve this answer














          If you have the truncate command and your text file has NL as its last character you can remove it and then append your text like this:



          truncate --size -1 file.txt
          echo "abc" >>file.txt


          (Note that truncate cares nothing about file content, and in this example simply reduces the file size by one byte. If your last character is not a single byte, ie it's a multi-byte "wide" character then you will introduce corruption.)







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 30 at 20:27

























          answered Dec 24 '17 at 20:49









          roaima

          39.8k545108




          39.8k545108




















              up vote
              5
              down vote













              Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g.



              echo -n "some text here" >> file.txt


              However, some UNIX systems do not provide this option; if that is the case you can use printf, e.g.



              printf %s "some text here" >> file.txt


              (the initial %s argument being to guard against the additional text having % formatting characters in it)



              From man echo (on macOS High Sierra):




              -n




              Do not print the trailing newline character. This may also be achieved by appending 'c' to the end of the string, as is done by iBCS2 compatible systems. Note that this option as well as the effect of 'c' are implementation-defined in IEEE Std 1003.1-2001 ("POSIX.1") as amended by Cor. 1-2002. Applications aiming for maximum portability are strongly encouraged to use printf(1) to suppress the newline character.








              share|improve this answer






















              • It quite obviously does end with a newline. echo -n would put no newline at the end of abc, but abc would still be preceded by a newline, which is what the user wants to avoid.
                – Kusalananda
                Jul 14 at 8:53










              • @Kusalananda My answer was more using the assumption that the OP would try to change their process such that the spurious n didn't show up in the first place. More options are better, especially when they don't involve having to rewrite the entire file every time a change happens (which can get rather slow and run in polynomial time if it's done repeatedly).
                – fluffy
                Jul 16 at 1:02














              up vote
              5
              down vote













              Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g.



              echo -n "some text here" >> file.txt


              However, some UNIX systems do not provide this option; if that is the case you can use printf, e.g.



              printf %s "some text here" >> file.txt


              (the initial %s argument being to guard against the additional text having % formatting characters in it)



              From man echo (on macOS High Sierra):




              -n




              Do not print the trailing newline character. This may also be achieved by appending 'c' to the end of the string, as is done by iBCS2 compatible systems. Note that this option as well as the effect of 'c' are implementation-defined in IEEE Std 1003.1-2001 ("POSIX.1") as amended by Cor. 1-2002. Applications aiming for maximum portability are strongly encouraged to use printf(1) to suppress the newline character.








              share|improve this answer






















              • It quite obviously does end with a newline. echo -n would put no newline at the end of abc, but abc would still be preceded by a newline, which is what the user wants to avoid.
                – Kusalananda
                Jul 14 at 8:53










              • @Kusalananda My answer was more using the assumption that the OP would try to change their process such that the spurious n didn't show up in the first place. More options are better, especially when they don't involve having to rewrite the entire file every time a change happens (which can get rather slow and run in polynomial time if it's done repeatedly).
                – fluffy
                Jul 16 at 1:02












              up vote
              5
              down vote










              up vote
              5
              down vote









              Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g.



              echo -n "some text here" >> file.txt


              However, some UNIX systems do not provide this option; if that is the case you can use printf, e.g.



              printf %s "some text here" >> file.txt


              (the initial %s argument being to guard against the additional text having % formatting characters in it)



              From man echo (on macOS High Sierra):




              -n




              Do not print the trailing newline character. This may also be achieved by appending 'c' to the end of the string, as is done by iBCS2 compatible systems. Note that this option as well as the effect of 'c' are implementation-defined in IEEE Std 1003.1-2001 ("POSIX.1") as amended by Cor. 1-2002. Applications aiming for maximum portability are strongly encouraged to use printf(1) to suppress the newline character.








              share|improve this answer














              Assuming that the file does not already end in a newline and you simply want to append some more text without adding one, you can use the -n argument, e.g.



              echo -n "some text here" >> file.txt


              However, some UNIX systems do not provide this option; if that is the case you can use printf, e.g.



              printf %s "some text here" >> file.txt


              (the initial %s argument being to guard against the additional text having % formatting characters in it)



              From man echo (on macOS High Sierra):




              -n




              Do not print the trailing newline character. This may also be achieved by appending 'c' to the end of the string, as is done by iBCS2 compatible systems. Note that this option as well as the effect of 'c' are implementation-defined in IEEE Std 1003.1-2001 ("POSIX.1") as amended by Cor. 1-2002. Applications aiming for maximum portability are strongly encouraged to use printf(1) to suppress the newline character.









              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 24 '17 at 21:36

























              answered Dec 24 '17 at 21:29









              fluffy

              42728




              42728











              • It quite obviously does end with a newline. echo -n would put no newline at the end of abc, but abc would still be preceded by a newline, which is what the user wants to avoid.
                – Kusalananda
                Jul 14 at 8:53










              • @Kusalananda My answer was more using the assumption that the OP would try to change their process such that the spurious n didn't show up in the first place. More options are better, especially when they don't involve having to rewrite the entire file every time a change happens (which can get rather slow and run in polynomial time if it's done repeatedly).
                – fluffy
                Jul 16 at 1:02
















              • It quite obviously does end with a newline. echo -n would put no newline at the end of abc, but abc would still be preceded by a newline, which is what the user wants to avoid.
                – Kusalananda
                Jul 14 at 8:53










              • @Kusalananda My answer was more using the assumption that the OP would try to change their process such that the spurious n didn't show up in the first place. More options are better, especially when they don't involve having to rewrite the entire file every time a change happens (which can get rather slow and run in polynomial time if it's done repeatedly).
                – fluffy
                Jul 16 at 1:02















              It quite obviously does end with a newline. echo -n would put no newline at the end of abc, but abc would still be preceded by a newline, which is what the user wants to avoid.
              – Kusalananda
              Jul 14 at 8:53




              It quite obviously does end with a newline. echo -n would put no newline at the end of abc, but abc would still be preceded by a newline, which is what the user wants to avoid.
              – Kusalananda
              Jul 14 at 8:53












              @Kusalananda My answer was more using the assumption that the OP would try to change their process such that the spurious n didn't show up in the first place. More options are better, especially when they don't involve having to rewrite the entire file every time a change happens (which can get rather slow and run in polynomial time if it's done repeatedly).
              – fluffy
              Jul 16 at 1:02




              @Kusalananda My answer was more using the assumption that the OP would try to change their process such that the spurious n didn't show up in the first place. More options are better, especially when they don't involve having to rewrite the entire file every time a change happens (which can get rather slow and run in polynomial time if it's done repeatedly).
              – fluffy
              Jul 16 at 1:02










              up vote
              4
              down vote













              What you want is to add it at the end of the last line, so just before the delimiter of that last line, so just before the last character of the file.



              With ksh93, you can do:



              echo abc 1<> file >#((EOF - 1))


              Where 1<> is the standard operator to open a file in read+write mode (and more importantly without truncation) on stdout and >#((...)) is a ksh93-specific seeking operator (here to seek to before the last character). Note that echo writes abc<newline> where the a overwrites the newline that was there and echo adds its own newline.



              The zsh equivalent:



              zmodload zsh/system
              sysseek -w end -u 1 -1; echo abc 1<> file





              share|improve this answer
























                up vote
                4
                down vote













                What you want is to add it at the end of the last line, so just before the delimiter of that last line, so just before the last character of the file.



                With ksh93, you can do:



                echo abc 1<> file >#((EOF - 1))


                Where 1<> is the standard operator to open a file in read+write mode (and more importantly without truncation) on stdout and >#((...)) is a ksh93-specific seeking operator (here to seek to before the last character). Note that echo writes abc<newline> where the a overwrites the newline that was there and echo adds its own newline.



                The zsh equivalent:



                zmodload zsh/system
                sysseek -w end -u 1 -1; echo abc 1<> file





                share|improve this answer






















                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  What you want is to add it at the end of the last line, so just before the delimiter of that last line, so just before the last character of the file.



                  With ksh93, you can do:



                  echo abc 1<> file >#((EOF - 1))


                  Where 1<> is the standard operator to open a file in read+write mode (and more importantly without truncation) on stdout and >#((...)) is a ksh93-specific seeking operator (here to seek to before the last character). Note that echo writes abc<newline> where the a overwrites the newline that was there and echo adds its own newline.



                  The zsh equivalent:



                  zmodload zsh/system
                  sysseek -w end -u 1 -1; echo abc 1<> file





                  share|improve this answer












                  What you want is to add it at the end of the last line, so just before the delimiter of that last line, so just before the last character of the file.



                  With ksh93, you can do:



                  echo abc 1<> file >#((EOF - 1))


                  Where 1<> is the standard operator to open a file in read+write mode (and more importantly without truncation) on stdout and >#((...)) is a ksh93-specific seeking operator (here to seek to before the last character). Note that echo writes abc<newline> where the a overwrites the newline that was there and echo adds its own newline.



                  The zsh equivalent:



                  zmodload zsh/system
                  sysseek -w end -u 1 -1; echo abc 1<> file






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 24 '17 at 22:02









                  Stéphane Chazelas

                  282k53518851




                  282k53518851




















                      up vote
                      -1
                      down vote













                      Probably a UUOC but you could also do:



                      echo "$(cat file.txt)abc" >file.txt


                      As Gilles points out this command is limited:




                      This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.




                      Additionally be wary of using cat on files you are not familiar with:



                      • CVE-2003-0063

                      • CVE-2008-2383

                      • CVE-2010-2713

                      • CVE-2003-0020

                      • CVE-2012-3515

                      • More

                      Conclusion:



                      Use sed






                      share|improve this answer


















                      • 2




                        This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.
                        – Gilles
                        Dec 24 '17 at 17:49










                      • Should I delete it? I definitely think Roman/your answer is the proper way to go but I know I personally like seeing alternatives, and OP did ask for echo :p
                        – Jesse_b
                        Dec 24 '17 at 17:51






                      • 1




                        Also this puts the entire file on the command line
                        – n.caillou
                        Dec 24 '17 at 19:34










                      • @n.caillou huh? This will not print anything to STDOUT.
                        – Jesse_b
                        Dec 24 '17 at 20:54






                      • 2




                        Well, none of those vulnerabilities are really related to cat, but to issues with arbitrary escape sequences being sent to the terminal.
                        – ilkkachu
                        Dec 24 '17 at 21:02














                      up vote
                      -1
                      down vote













                      Probably a UUOC but you could also do:



                      echo "$(cat file.txt)abc" >file.txt


                      As Gilles points out this command is limited:




                      This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.




                      Additionally be wary of using cat on files you are not familiar with:



                      • CVE-2003-0063

                      • CVE-2008-2383

                      • CVE-2010-2713

                      • CVE-2003-0020

                      • CVE-2012-3515

                      • More

                      Conclusion:



                      Use sed






                      share|improve this answer


















                      • 2




                        This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.
                        – Gilles
                        Dec 24 '17 at 17:49










                      • Should I delete it? I definitely think Roman/your answer is the proper way to go but I know I personally like seeing alternatives, and OP did ask for echo :p
                        – Jesse_b
                        Dec 24 '17 at 17:51






                      • 1




                        Also this puts the entire file on the command line
                        – n.caillou
                        Dec 24 '17 at 19:34










                      • @n.caillou huh? This will not print anything to STDOUT.
                        – Jesse_b
                        Dec 24 '17 at 20:54






                      • 2




                        Well, none of those vulnerabilities are really related to cat, but to issues with arbitrary escape sequences being sent to the terminal.
                        – ilkkachu
                        Dec 24 '17 at 21:02












                      up vote
                      -1
                      down vote










                      up vote
                      -1
                      down vote









                      Probably a UUOC but you could also do:



                      echo "$(cat file.txt)abc" >file.txt


                      As Gilles points out this command is limited:




                      This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.




                      Additionally be wary of using cat on files you are not familiar with:



                      • CVE-2003-0063

                      • CVE-2008-2383

                      • CVE-2010-2713

                      • CVE-2003-0020

                      • CVE-2012-3515

                      • More

                      Conclusion:



                      Use sed






                      share|improve this answer














                      Probably a UUOC but you could also do:



                      echo "$(cat file.txt)abc" >file.txt


                      As Gilles points out this command is limited:




                      This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.




                      Additionally be wary of using cat on files you are not familiar with:



                      • CVE-2003-0063

                      • CVE-2008-2383

                      • CVE-2010-2713

                      • CVE-2003-0020

                      • CVE-2012-3515

                      • More

                      Conclusion:



                      Use sed







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 24 '17 at 17:59

























                      answered Dec 24 '17 at 17:40









                      Jesse_b

                      10.5k22659




                      10.5k22659







                      • 2




                        This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.
                        – Gilles
                        Dec 24 '17 at 17:49










                      • Should I delete it? I definitely think Roman/your answer is the proper way to go but I know I personally like seeing alternatives, and OP did ask for echo :p
                        – Jesse_b
                        Dec 24 '17 at 17:51






                      • 1




                        Also this puts the entire file on the command line
                        – n.caillou
                        Dec 24 '17 at 19:34










                      • @n.caillou huh? This will not print anything to STDOUT.
                        – Jesse_b
                        Dec 24 '17 at 20:54






                      • 2




                        Well, none of those vulnerabilities are really related to cat, but to issues with arbitrary escape sequences being sent to the terminal.
                        – ilkkachu
                        Dec 24 '17 at 21:02












                      • 2




                        This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.
                        – Gilles
                        Dec 24 '17 at 17:49










                      • Should I delete it? I definitely think Roman/your answer is the proper way to go but I know I personally like seeing alternatives, and OP did ask for echo :p
                        – Jesse_b
                        Dec 24 '17 at 17:51






                      • 1




                        Also this puts the entire file on the command line
                        – n.caillou
                        Dec 24 '17 at 19:34










                      • @n.caillou huh? This will not print anything to STDOUT.
                        – Jesse_b
                        Dec 24 '17 at 20:54






                      • 2




                        Well, none of those vulnerabilities are really related to cat, but to issues with arbitrary escape sequences being sent to the terminal.
                        – ilkkachu
                        Dec 24 '17 at 21:02







                      2




                      2




                      This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.
                      – Gilles
                      Dec 24 '17 at 17:49




                      This mostly works, but not if there is sometimes a blank line at the end of the file and a blank line immediately before. E.g. a file with a fixed number of lines, where the last line is initially empty and is extended over time, and the next-to-last-line may sometimes be empty.
                      – Gilles
                      Dec 24 '17 at 17:49












                      Should I delete it? I definitely think Roman/your answer is the proper way to go but I know I personally like seeing alternatives, and OP did ask for echo :p
                      – Jesse_b
                      Dec 24 '17 at 17:51




                      Should I delete it? I definitely think Roman/your answer is the proper way to go but I know I personally like seeing alternatives, and OP did ask for echo :p
                      – Jesse_b
                      Dec 24 '17 at 17:51




                      1




                      1




                      Also this puts the entire file on the command line
                      – n.caillou
                      Dec 24 '17 at 19:34




                      Also this puts the entire file on the command line
                      – n.caillou
                      Dec 24 '17 at 19:34












                      @n.caillou huh? This will not print anything to STDOUT.
                      – Jesse_b
                      Dec 24 '17 at 20:54




                      @n.caillou huh? This will not print anything to STDOUT.
                      – Jesse_b
                      Dec 24 '17 at 20:54




                      2




                      2




                      Well, none of those vulnerabilities are really related to cat, but to issues with arbitrary escape sequences being sent to the terminal.
                      – ilkkachu
                      Dec 24 '17 at 21:02




                      Well, none of those vulnerabilities are really related to cat, but to issues with arbitrary escape sequences being sent to the terminal.
                      – ilkkachu
                      Dec 24 '17 at 21:02












                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412835%2fappend-text-with-echo-without-new-line%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      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?