How can you you insert a newline from your batch file output?
I want to do something like:
> echo hello\nworld
Which would output:
hello
world
How can you you insert a newline from your batch file output?
I want to do something like:
> echo hello\nworld
Which would output:
hello
world
This worked for me, no delayed expansion necessary:
It writes output like this:
The most character conservative way to accomplish this in linux systems, is with the
-e
alias - which interprets escaped characters that are backslashed.Here are other examples
Here you go, create a .bat file with the following in it :
You should see output like the following:
You only need the code between the REM statements, obviously.
echo hello & echo.world
This means you could define
& echo.
as a constant for a newline\n
.echo.
Enough said.If you need it in a single line, use the
&
. For example,would output as:
Now, say you want something a bit fancier, ...
Outputs
Then just throw in a
%n%
whenever you want a new line in an echo statement. This is more close to your\n
used in various languages.Breakdown
set n=
sets the variablen
equal to:^
Nulls out the next symbol to follow:&
Means to do another command on the same line. We don't care about errorlevel(its an echo statement for crying out loud), so no&&
is needed.echo.
Continues the echo statement.All of this works because you can actually create variables that are code, and use them inside of other commands. It is sort of like a ghetto function, since batch is not exactly the most advanced of shell scripting languages. This only works because batch's poor usage of variables, not designating between ints, chars, floats, strings, etc naturally.
If you are crafty, you could get this to work with other things. For example, using it to echo a tab
To start a new line in batch, all you have to do is add "echo[", like so: