@echo off in cmd

2019-06-16 08:40发布

I'm trying to write a BAT script and I have the following:

@echo off
REM Comments here
SETLOCAL ENABLEDELAYEDEXPANSION
set PROG_ROOT=C:\Prog
set ONE=1

echo 1>> %PROG_ROOT\test.txt
echo %ONE%>> %PROG_ROOT\test.txt

for /f "tokens=*" %%f in (folders.txt) do (
    echo %%f>> %PROG_ROOT\test.txt
)

ENDLOCAL

My folders.txt contains the number "5".

My test.txt output is

ECHO is off
ECHO is off
5

I don't understand why the first 2 lines of output has "ECHO is off", while the third line is printed out correctly. How do I print the correct output?

ETA: I tried

echo 1>> %PROG_ROOT\test.txt
echo %ONE% >> %PROG_ROOT\test.txt

and I was able to print

ECHO is off
1

However, I need to NOT print the trailing space after the number.

标签: windows cmd echo
2条回答
爷的心禁止访问
2楼-- · 2019-06-16 09:25
echo 1>> %PROG_ROOT\test.txt
echo %ONE%>> %PROG_ROOT\test.txt

for /f "tokens=*" %%f in (folders.txt) do (
    echo %%f>> %PROG_ROOT\test.txt
)

ENDLOCAL
查看更多
We Are One
3楼-- · 2019-06-16 09:26

1> (and more generally n> for any digit n) is interpreted as a redirection, and thus echo 1>> appears to cmd as an echo with no arguments. echo with no arguments will print the current echo state (here, ECHO is off).

To fix, escape the integer with a ^ character:

echo ^1>> %PROG_ROOT\test.txt
查看更多
登录 后发表回答