windows @echo 在 cmd 中关闭

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12505754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:53:54  来源:igfitidea点击:

@echo off in cmd

windowscmdecho

提问by Rayne

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

我正在尝试编写一个 BAT 脚本,我有以下内容:

@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".

我的 folders.txt 包含数字“5”。

My test.txt output is

我的 test.txt 输出是

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?

我不明白为什么输出的前 2 行显示“ECHO is off”,而第三行打印正确。如何打印正确的输出?

ETA: I tried

ETA:我试过了

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.

但是,我不需要在数字后打印尾随空格。

采纳答案by nneonneo

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

1>(更一般地n>用于任何 digit n)被解释为重定向,因此echo 1>>显示cmdecho没有参数。echo不带参数将打印当前echo状态(此处为ECHO is off)。

To fix, escape the integer with a ^character:

要修复,请使用^字符转义整数:

echo ^1>> %PROG_ROOT\test.txt

回答by Nguy?n L?i

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