How to insert a carriage return with batch

Sometimes the quickest way to accomplish something in Windows is with a simple batch file. The best example would be when you want to run a command and append the contents of it to a text file for later perusal. Occasionally, I’ve had a desire to run multiple commands and append them to the same file, but I like to separate the output from the various commands by some whitespace to make it easier to look at later. I’ve always just echo’d some arbitrary character to the file to cause blank lines to show up, like this: echo _ >> somefile.txt

I realized today that there’s actually an easy way to simply echo a carriage return or line feed instead, by just issuing an echo command followed immediately by a period (no space in between), like this:

echo. >> somefile.txt

Thought I’d share it with the rest of you who didn’t know this already.

Other articles you might find useful:

4 comments:

  1. Matias, 20. March 2008, 7:15

    Thank you very much, I needed this :)

     
  2. Andrew from Vancouver, 4. April 2008, 16:11

    Ahhh, but why does it work? I’ve used it for years and years and regarded it as a tweak that just worked.

    But there is a reason! By design, cmd.exe will interpret what you “echo”. So, to get it to just darn well echo what you’re telling it to, but still be able to pass parameters, you need a way to tell it which is which.

    All credit goes to Raymond Chen at his blog:

    http://blogs.msdn.com/oldnewthing/archive/2008/04/03/8352719.aspx

     
  3. mister jones, 5. April 2008, 5:49

    Matias: You’re welcome!

    Andrew: Excellent information! I had never thought before of why it worked. Nice to gain a little revelation every once in a while and realize that something like that actually has some reasoning behind it :) Great article from Raymond Chen. Thanks for sharing!

    From the comments on Raymond’s post, it looks like people were trying to suggest a similar method for doing the same thing with echo on a *nix system. I don’t know if this is the case on all *nix’es, but at least on Slackware and Debian distributions of Linux, I’m able to accomplish a blank line/carriage return by simply using: echo >> somefile (echo followed by nothing).

     
  4. Adam, 2. May 2008, 16:47

    Why did it take me so long to find this??? I have been for an hour trying to find out how to perform a carriage return in DOS using the “echo” command. I have tried
    echo “Hello\nWorld”
    and using the -e and \n and \\n but DOS always interprets it literally. If anyone can help, please let me know. This worked to create a new line for me, thanks.