How do I give an echo without breaking line?

Asked

Viewed 3,736 times

4

I have this line in the file .bat

for /L %%a in (1,1,3)DO echo %%a

Printing:

1

2

3

but I want you to print on the same line, this way:

1 2 3

1 answer

4

A great technical repair 1 is to use the set with the parameter /P to avoid line break.

Both batches then produce the desired effect:

@echo off
for /L %%a in (1,1,3)DO echo|set /p="%%a "

and

@echo off
for /L %%a in (1,1,3)DO <nul set /p="%%a "

The /p is usually used to input with a prompt, and so does not break any lines. Matching the input with an artificial input, which comes from a echo with pipe in the first example and a nul in place of stdin of the second, we have the effect of showing the prompt without breaking lines, and without waiting for the input user’s.

The second option apparently has higher performance, which can be evaluated with more extensive testing (larger loops, for example).


1. Gambiarra

Browser other questions tagged

You are not signed in. Login or sign up in order to post.