7

I want to print the output of a program in MS-DOS so I wrote a .bat file that says:

cls
ruby foo.rb

But the output - as it appears on my command prompt - looks like this:

c:\workspace>ruby foo.rb
foo output
c:\workspace>

I wanted to insert a newline into the output using MS-DOS because I don't want to pollute my Ruby code with anything not related to what the code is supposed to be doing.

The only commands in MS-DOS that look like what I want are 'type' and 'print' but both are for printing files.

I tried creating a text file with two blank lines and outputting it using the 'type' command but it looks messy.

Any ideas would be appreciated.

Dana
  • 32,083
  • 17
  • 62
  • 73
Bryan Locke
  • 2,337
  • 5
  • 25
  • 30
  • The canonical question is *[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:20
  • @PeterMortensen Despite the similarity of titles, that's a different question. This question asks how to output a blank line. The question you linked asks how to output two words each on their own line. – Ross Ridge Oct 22 '15 at 01:00

7 Answers7

27

echo. will produce a new line.

So your script should look something like this:

@ECHO OFF
cls
echo.
ruby foo.rb
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John T
  • 23,735
  • 11
  • 56
  • 82
6

Even if here are 4 answers with the same tip to use ECHO., it's not the best solution!

There are two drawbacks.

  1. It's slow
    ECHO. is ~10 times slower than a normal echo command, that's because ECHO. will force a disk access

  2. It can fail
    If a file exists with the name ECHO (without extension) then each ECHO. command will result in a file not found error, instead of echoing an empty line.

But what do you can do then?
For a simple empty line, there are many working ways.

echo+
echo=
echo;
echo,
echo:
echo(

But sometimes you want a secure way to echo the content of a variable even if the variable is empty or contain odd content like /? or \\..\..\windows\system32\calc.exe.

ECHO<character>%variable%

echo=/?
echo;/?
echo,/?
echo:\\..\..\windows\system32\calc.exe

Then the most commands will fail, only ECHO( works in any situation.
It looks a little bit strange, but it works and it does not need nor make any trouble with a closing bracket.

jeb
  • 78,592
  • 17
  • 171
  • 225
4

how about:

@echo off
cls
echo.
ruby foo.rb
echo.

bye

Edoardo Vacchi
  • 1,284
  • 10
  • 18
3

Try this

echo.
Alex Reitbort
  • 13,504
  • 1
  • 40
  • 61
2

Use the echo command followed by a period to display a new line in an MS-DOS batch file:

echo.
ahockley
  • 3,696
  • 24
  • 26
1

I agree with jeb. "echo(" is definitely faster and does not invoke a file access. Just try it and you'll know for yourself. Here is a little test you might want to try out.

Create a file named "echo" and in the same directory create a test.bat or test.cmd file have the following content in the script:

@echo off
echo %time%
echo.
echo Hello, World
echo.
echo.
echo What time is it?
echo.
echo It's Miller Time!
echo.
echo.
echo CHEERS!
echo.
echo %time%

Run the newly created batch file. What do you get? A bunch of error messages when a file "echo" exists in the same directory.

Now, delete the "echo" file.

On my system, it took approximately .05 seconds from start to finish using the "echo." method due to file access taking place.

Now, replace all "echo." in the test file above with "echo(" and run it again.

It takes approximately .01 seconds from start to finish using the "echo(" statement.

Final test, reintroduce a file called "echo" in the same directory and run the batch one last time.

No error messages.

QED

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
ljm13
  • 11
  • 2
0

I'm not clear on what you want here, but maybe this will help.

If you want the output sent to somewhere else, use the dos output "pipe".

ruby foo.rb > out.txt

will output the output of the ruby command to the out.txt file.

If you want to control the output, use ECHO.

@ECHO OFF/ON //turns off/on command output
ECHO "Blah" //writes "Blah" to the console.
ECHO. //writes a blank line to the console.
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
StingyJack
  • 19,041
  • 10
  • 63
  • 122
  • 1
    That last one does not echo a blank line. It echo's "on" or "off" depending on the state set by the first line. To get a blank line you have to `echo.` as others have pointed out. – beltorak May 25 '11 at 14:20