如何回显“2” (无引号)从批处理脚本到文件?

发布于 2024-12-01 17:53:42 字数 150 浏览 11 评论 0原文

如何从批处理脚本将数字 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

梦里人 2024-12-08 17:53:42

鲜为人知的功能:重定向操作符可以到达线路上的任何位置。

>>file.txt echo 2

Little-known feature: The redirection operator can go anywhere on the line.

>>file.txt echo 2
笑叹一世浮沉 2024-12-08 17:53:42

使用(ECHO 2)>>file.txt。这将输出不带任何空格的2

Use (ECHO 2)>>file.txt. This will output 2 without any spaces.

所谓喜欢 2024-12-08 17:53:42

echo ^2>>file.txt 似乎对我有用。

echo ^2>>file.txt appears to work for me.

友欢 2024-12-08 17:53:42

使用 ^ 转义:

Echo ^2>> file.txt

Use the ^ escape :

Echo ^2>> file.txt
Spring初心 2024-12-08 17:53:42
echo.2>>text.txt

是的,这很奇怪。

echo.2>>text.txt

Yes, it's weird.

饮惑 2024-12-08 17:53:42

另一种方法

echo>>file.txt 2

another method

echo>>file.txt 2
妄司 2024-12-08 17:53:42

或者你可以使用延迟扩展

setlocal EnableDelayedExpansion
set "var=2"
echo(!var!>> file.txt

Or you can use the delayed expansion

setlocal EnableDelayedExpansion
set "var=2"
echo(!var!>> file.txt
世界等同你 2024-12-08 17:53:42

如果需要精确写入,请使用此命令,

(ECHO |set /p=2)>>file.txt

DMan 的命令将在“2”后面产生 0x0D0A 换行符

If you need an accurate write, use this command

(ECHO |set /p=2)>>file.txt

the command by DMan will produce a 0x0D0A line break behind the "2"

七秒鱼° 2024-12-08 17:53:42

要将数字写入没有尾随换行符的文件,您可以使用以下命令:

cmd /C set /A "2">file.txt

这有效,因为在命令提示符下执行时 set /A 返回(最后)结果(没有换行符)上下文(因此当直接在 cmd.exe 中运行时)。

如果您无论如何都在命令提示符下工作,则可以简单地使用它:

set /A "2">file.txt

虽然您不能在批处理文件中使用它,但您需要额外的 cmd /C 来强制正确的执行上下文。

不用说,这当然只能输出数字,或者准确地说,有符号的 32 位整数。

To write a number to a file without a trailing line-break you could use the following:

cmd /C set /A "2">file.txt

This works, because set /A returns the (last) result (without line-break) when executed in command prompt context (hence when directly run in cmd.exe).

If you are working in command prompt anyway, you could simply use this:

set /A "2">file.txt

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.

溺渁∝ 2024-12-08 17:53:42

根据这个答案,如果有人想知道如何执行此操作:

echo "command1 && command2" > file.txt

file.txt 然后你可以这样做:

echo command1 ^&^& command2 > file.txt

在 Windows Server 2003 上测试。

Based on this answer, if anyone is wondering how to do this:

echo "command1 && command2" > file.txt

with no qoutes in file.txt then you do it like this:

echo command1 ^&^& command2 > file.txt

Tested on Windows Server 2003.

眼眸 2024-12-08 17:53:42

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文