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

42,524

Solution 1

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

Solution 2

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

Share:
42,524
Arunachalam
Author by

Arunachalam

fun

Updated on July 09, 2022

Comments

  • Arunachalam
    Arunachalam almost 2 years

    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?

  • Kzqai
    Kzqai about 14 years
    Hmmm, that didn't work for me in ubuntu, recognized echo. as an unfound command.
  • palantus
    palantus about 14 years
    @Tchalvak: This is a Windows question as far as we can tell from the clues in the question.
  • Kzqai
    Kzqai about 14 years
    Ah, Fair enough. What an annoying implementation of the command.
  • Rich
    Rich about 14 years
    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.
  • palantus
    palantus about 14 years
    Thanks, @Johannes. I didn't look closely enough.
  • Inversus
    Inversus over 11 years
    That will output the string literal "\n" to the file
  • Duncan Howe
    Duncan Howe about 2 years
    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)