19

How do I append an empty line in a text file using the command line?

 echo hi >a.txt
    echo >>a.txt
    echo arun >>a.txt

Here the output comes as:

hi
echo on
arun

So how could I append an empty line? I want it to be like this:

hi

arun

When I added this line on code @echo off, it said echo off. How can it be done?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arunachalam
  • 5,417
  • 20
  • 52
  • 80
  • 2
    I added the windows tag, since that's the only platform I can think of that would say "echo on" in response to an unqualified "echo" command. When asking questions its good to include the platform and/or language so the right people see your question. Also, Use the 010101 button to format your code. – Laurence Gonsalves Mar 31 '10 at 16:59
  • Possible duplicate of *[How can you echo a newline in batch files?](http://stackoverflow.com/questions/132799/how-can-you-echo-a-newline-in-batch-files)*. – Peter Mortensen Oct 21 '15 at 09:25

3 Answers3

35

In the Windows command prompt, try:

echo.>> a.txt

Note that there is no space between echo and .; if there is one, it will output a dot. There is also no space between the . and the >>; anything in between would be output to the file, even whitespace characters.

See the Microsoft documentation for echo.

If this were in bash, your first try would have been correct:

echo >> a.txt

But in Windows, the unqualified echo command tests whether there is a command prompt or not (echo off turns the prompt off and echo on turns it back on).

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
  • Hmmm, that didn't work for me in ubuntu, recognized echo. as an unfound command. – Kzqai Mar 31 '10 at 17:02
  • @Tchalvak: This is a Windows question as far as we can tell from the clues in the question. – Michael Myers Mar 31 '10 at 17:04
  • 1
    Ah, Fair enough. What an annoying implementation of the command. – Kzqai Mar 31 '10 at 17:05
  • There shouldn't be a space between the dot and the redirection operator. Otherwise this will output a space followed by a line break. For some definitions of “empty line” that's not exactly empty. – Joey Mar 31 '10 at 18:37
  • Thanks, @Johannes. I didn't look closely enough. – Michael Myers Mar 31 '10 at 18:58
  • Some 12 years later, I found that in a batch file, with `@echo OFF` doing this writes `ECHO IS OFF` rather than a blank line. If you add a space between the dot and the double chevron then it works as expected (i.e. `echo. >> file.txt`) – Duncan Howe Apr 28 '22 at 11:40
6

At Windows Prompt:

echo. >> a.txt

At BASH Prompt:

echo >> a.txt

(Echo by default sends a trailing newline)

-n do not output the trailing newline

jjclarkson
  • 5,890
  • 6
  • 40
  • 62
-4

It should be possible with a simple

echo hi\n >> a.txt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vishal Seth
  • 4,948
  • 7
  • 26
  • 28