echo, newline escaping, and more

  • Thread starter Thread starter Csaba Gabor
  • Start date Start date
C

Csaba Gabor

My question is how to pipe newlines directly
from echo into more.

Detailed explanation:
It is possible to escape newlines in cmd.exe
by ending a line with the caret (^) and
then pressing the enter key twice. Thus,
the following 3 lines will display foo, and
on the next line it will display bar:

echo foo^

bar


Very nifty. What I would now like, however,
is for the result of an echo, piped into
more, to display foo on one line, followed
by bar on the next. The following 3 lines
do not work:

echo foo^

bar | more


more will only shows the first line, foo.
What is going on is that when echo is piped
into more, for reasons I have not fathomed,
the command line parser runs over the
command line twice (for example, there are
two levels of environment variable expansion
as will be seen shortly). Therefore, it
would seem that I have to escape the above 3
lines once to the following 5 lines (using
double caret below (^^) to escape a single
caret (^) above and ^ followed by two newlines
below to escape a single newline above):

echo foo^^^

^

bar | more



Unfortunately, this does not work, and as
before, I only get the first line showing:

foo

Furthermore, if the last line is only bar
instead of bar | more, then the 3 line
output from echo's single pass is as
expected:

foo^

bar



Now you will be thinking, Csaba, really,
you were overly optimistic thinking that
the command parser would be willing to
accommodate that screwy newline escaping.
You should be lucky that it worked at all
with echo, never mind the pipe.

Well, perhaps so, but then I would expect
the following 5 lines to similarly fail,
but it does quite nicely, thank you very
much.

set nl=^


set nlm=^^^^%^nl%%^nl%
echo foo%nlm%bar | more


The 'echo ... | more' demonstrates
the double substitution mentioned earlier.
The last two lines would also work as:

set nlm=^^^%nl%%nl%^%nl%%nl%
echo foo%^nlm%bar | more

which 2nd variant causes a single expansion
on the 2nd pass.


But I wonder if it is possible to avoid
the expansion on the second pass, which
happens in both examples. In other words,
could I have an environment variable
just expand on the first pass? And this
should be the same as my original question:
How can I pipe newlines directly from
echo into more?


Csaba Gabor from Vienna


PS. The above may be tested by copying
the specified number of lines and then
at the cmd.exe prompt doing (Alt+space)ep
(enter may need to be pressed depending
on whether the copied section has a final
newline in it)

PPS. Environment variable expansion
happens first, and then escaped characters
are unescaped. Thus, an isolated ^ in an
environment variable will seemingly
disappear.

PPPS. VBScript group copied as a possilble
interest topic when using Run/Exec
 
My question is how to pipe newlines directly
from echo into more.

Detailed explanation:
It is possible to escape newlines in cmd.exe
by ending a line with the caret (^) and
then pressing the enter key twice. Thus,
the following 3 lines will display foo, and
on the next line it will display bar:

echo foo^

bar


Very nifty. What I would now like, however,
is for the result of an echo, piped into
more, to display foo on one line, followed
by bar on the next. The following 3 lines
do not work:

echo foo^

bar | more

Is this useful?

@echo off
(
echo foo
echo.
echo bar
)|more
 
foxidrive said:
Is this useful?

@echo off
(
echo foo
echo.
echo bar
)|more

Fabulous!
From the command line I can now do:

(echo foo && echo bar) | more
(echo.foo&&echo.bar)|more

Not only that, but it also gives the same
output without the piping:

(echo foo && echo bar)

Furthermore, I'd been wanting a way to
make a totally blank prompt/page under cmd.exe
and this is an easy way:

(@echo off && cls)


Thanks very much for your suggestion Foxi!
Csaba Gabor from Vienna

PS. The results are meant for use with a
single invocation of the command line.
 
I often use compound commands like that when I want a batch script to
generate a log file of its proceedings, as I think that this:

(
echo/starting at:
time /t
call:process
echo/done at
time /t
) >logfile.log

is more efficient than multiple instances of the ">>" redirection operator,
in addition to being simpler to get right.

/Al
 
Back
Top