37

I have a shell script that I am executing in Cygwin (maybe this is the problem). For this bit of code, I simply want to write the first line, and append a line break:

echo "`date` User `whoami` started the script." >> output.log
echo >> output.log

But the output.log file never seems to take the break. If I run the script multiple times, it's as if the second echo doesn't write to the file.

I've also tried:

echo -e "`date` User `whoami` started the script.\n" >> output.log

It yields the same results.

The odd thing is if I just enter the second echo statement above on the command line, without appending to the file, it gives me the expected output with the trailing line break.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ode
  • 465
  • 1
  • 5
  • 12
  • Strange. How do you determine that the newlines are not added? – Michał Kosmulski Mar 16 '12 at 17:03
  • Actually, I just looked at it seems the program I'm opening the log in is not interpreting the LF correctly, and is expecting Windows line breaks. If I cat the output, it seems to honor the breaks. – Ode Mar 16 '12 at 18:41

5 Answers5

37

I'm betting the problem is that Cygwin is writing Unix line endings (LF) to the file, and you're opening it with a program that expects Windows line-endings (CRLF). To determine if this is the case — and for a bit of a hackish workaround — try:

echo "`date` User `whoami` started the script."$'\r' >> output.log

(where the $'\r' at the end is an extra carriage-return; it, plus the Unix line ending, will result in a Windows line ending).

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • This worked perfectly. The hypothesis about Unix vs Windows line endings seemed to be the problem – Ode Mar 16 '12 at 18:44
24

Try:

echo "`date` User `whoami` started the script."$'\n' >> output.log

or just:

echo $'\n' >> output.log
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • Note that command-substitution is not performed within `$'...'`, so that version will print the actual string, backticks and "date" and "whoami" and all, rather than running the `date` and `whoami` commands. – ruakh Mar 16 '12 at 17:07
  • @ruakh - Good catch. I did not notice that. I updated my answer. – Daniel Haley Mar 16 '12 at 17:12
  • 1
    The "$'\n' did not work, needed to do the "$'\r' from ruakh's solution. Thanks so much though! – Ode Mar 16 '12 at 18:44
5

Try

echo -en "`date` User `whoami` started the script.\n" >> output.log

Try issuing this multiple times. I hope you are looking for the same output.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Tanbir
  • 63
  • 1
  • 6
5

this also works, and prolly is more readable than the echo version:

printf "`date` User `whoami` started the script.\r\n" >> output.log
eadmaster
  • 1,347
  • 13
  • 23
3

You can do that without an I/O redirection:

sed -i 's/$/\n/' filename

You can also use this command to append a newline to a list of files:

find dir -name filepattern | xargs sed -i 's/$/\n/' filename

For echo, some shells implement it as a shell builtin command. It might not accept the -e option. If you still want to use echo, try to find where the echo binary file is, using which echo. In most cases, it is located in /bin/echo, so you can use /bin/echo -e "\n" to echo a new line.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
StarPinkER
  • 14,081
  • 7
  • 55
  • 81