如何将 Windows 批处理文件中从 0 到 9 的数字回显到文件中而不尾随 space?

How to echo a number from 0 to 9 in a Windows batch file to a file without trailing space?

如果我在名为 test 的批处理文件中执行以下操作:

echo number=0 > file

和运行它:

> test
> echo number=0  1> file

file 包含:

number=0␣

如果我将 test 更改为:

echo number=1> file

和运行它:

 > test
 > echo number= 1>file

file 包含:

number=

1显然被解释为stdouthandle,这是可以理解的。

如果我将 test 更改为:

echo number=0> file

——和2分别到9——和运行它:

 > test
 > echo number= 0>file
 number=

029 显然被解释为非 stdout handlesfile 是空的,这是也可以理解。

有:

echo "number=1"> file

file 包含:

"number=1"

有:

echo (number=1)> file

file 包含:

(number=1)

在写问题时我在 How to avoid writing trailing spaces into text file on redirecting ECHO outputs to a file?:

找到了答案

将重定向放在命令前面,如:

> file echo number=0

或者,正如 Stephan 评论的那样,group 完整的命令,而不仅仅是参数:

(echo number=0) > file

(标题 Using parenthesis/brackets to group expressions 有点遗漏,恕我直言。它 命令和 表达式 组合在一起,不是吗?)