Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a newline in an MS-DOS script?

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.

like image 929
Bryan Locke Avatar asked Feb 23 '09 16:02

Bryan Locke


3 Answers

echo. will produce a new line.

So your script should look something like this:

@ECHO OFF
cls
echo.
ruby foo.rb
like image 142
John T Avatar answered Nov 12 '22 21:11

John T


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.

like image 20
jeb Avatar answered Nov 12 '22 21:11

jeb


how about:

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

bye

like image 43
Edoardo Vacchi Avatar answered Nov 12 '22 20:11

Edoardo Vacchi