21

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?

2
  • 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. Commented Mar 31, 2010 at 16:59
  • Possible duplicate of How can you echo a newline in batch files?. Commented Oct 21, 2015 at 9:25

4 Answers 4

37

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).

Sign up to request clarification or add additional context in comments.

5 Comments

Hmmm, that didn't work for me in ubuntu, recognized echo. as an unfound command.
@Tchalvak: This is a Windows question as far as we can tell from the clues in the question.
Ah, Fair enough. What an annoying implementation of the command.
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.
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)
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

Comments

1

I've battling with this for a while here's a tip of how i fixed it:

On you batch, put "echo(>> textfile.txt" (without quotation) without spaces between the echo command and the redirector or you'll get a line with a space. Not ideal if you're compiling a file to paste somewhere like excel and need the cell to be empty like i did for whatever reason.

Hope it helps.

Comments

-4

It should be possible with a simple

echo hi\n >> a.txt

1 Comment

That will output the string literal "\n" to the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.