Append on the same line bash

10,724

Solution 1

This is because your output Reboot done on with echo and then you execute date and put its output into the file. Try this instead:

echo "Reboot done on $(date)" > test.txt

Solution 2

Try this

echo -n "Reboot done on `date`" > test.txt

Solution 3

Here's another way:

(echo -n "Reboot done on "; date) > test.txt
Share:
10,724
dee doo
Author by

dee doo

Updated on June 04, 2022

Comments

  • dee doo
    dee doo almost 2 years

    Using bash, I need to add to the end of a file, on the same line a string followed by the date. Example "Rebooted on Mon Aug 13 10:38:56 PDT 2012"

    echo -n "Reboot done on " ; date
    

    This gives me what I want on one line, but I can't append to the text file.

    echo -n "Reboot done on " ; date > test.txt
    

    This does not seem to work for me. It only gives me the date.

    Thanks in advance.

    • chepner
      chepner over 11 years
      You might want '>>' instead of '>' here if you truly want to append to the file, not overwrite it.
    • dee doo
      dee doo over 11 years
      Thanks for catching that. Although, I was aware of that, just my question was formated poorly.
  • chepner
    chepner over 11 years
    { echo -n "..."; date; } would also concatenate the output streams, without spawning a subshell.