如何回显“2” (无引号)从批处理脚本到文件?
如何从批处理脚本将数字 2 回显到文件中?
这是行不通的:
Echo 2>> file.txt
因为 2>>
是一个特殊的命令。 :(
How do I echo the number 2 into a file, from a batch script?
This doesn't work:
Echo 2>> file.txt
because 2>>
is a special command. :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
鲜为人知的功能:重定向操作符可以到达线路上的任何位置。
Little-known feature: The redirection operator can go anywhere on the line.
使用
(ECHO 2)>>file.txt
。这将输出不带任何空格的2
。Use
(ECHO 2)>>file.txt
. This will output2
without any spaces.echo ^2>>file.txt
似乎对我有用。echo ^2>>file.txt
appears to work for me.使用
^
转义:Use the
^
escape :是的,这很奇怪。
Yes, it's weird.
另一种方法
another method
或者你可以使用延迟扩展
Or you can use the delayed expansion
如果需要精确写入,请使用此命令,
DMan 的命令将在“2”后面产生 0x0D0A 换行符
If you need an accurate write, use this command
the command by DMan will produce a 0x0D0A line break behind the "2"
要将数字写入没有尾随换行符的文件,您可以使用以下命令:
这有效,因为在命令提示符下执行时
set /A
返回(最后)结果(没有换行符)上下文(因此当直接在 cmd.exe 中运行时)。如果您无论如何都在命令提示符下工作,则可以简单地使用它:
虽然您不能在批处理文件中使用它,但您需要额外的
cmd /C
来强制正确的执行上下文。不用说,这当然只能输出数字,或者准确地说,有符号的 32 位整数。
To write a number to a file without a trailing line-break you could use the following:
This works, because
set /A
returns the (last) result (without line-break) when executed in command prompt context (hence when directly run incmd.exe
).If you are working in command prompt anyway, you could simply use this:
Though you cannot use that in a batch file, you need the extra
cmd /C
to force the right execution context.Needless to say, this can of course only output numbers, or precisely said, signed 32-bit integers.
根据这个答案,如果有人想知道如何执行此操作:
file.txt 然后你可以这样做:
在 Windows Server 2003 上测试。
Based on this answer, if anyone is wondering how to do this:
with no qoutes in
file.txt
then you do it like this:Tested on Windows Server 2003.
在 Linux 中转义字符“2”
:
echo \2>file.txt
在 Windows 中:
echo ^2>file.txt
在 Windows 中这也可以工作
echo.2>file.txt
echo 和点之间没有空格escape the haracter '2'
in linux:
echo \2>file.txt
in windows:
echo ^2>file.txt
in windows this also will work
echo.2>file.txt
no space between echo and the dot