如何使用 Windows 命令行在长一行中设置变量?

发布于 2024-10-03 14:05:11 字数 155 浏览 0 评论 0原文

例如:

我想设置一个变量,然后将其输出在同一行。

设置 /p MESSAGE= && echo %MESSAGE%

但它并不像您期望的那样工作。有没有办法通过管道来回显,或者有没有更好的方法来分隔单独的命令?

谢谢。

For example:

I want to set a variable and then have it output on the same line.

set /p MESSAGE= && echo %MESSAGE%

But it doesn't quite work like you expect. Is there a way to pipe it to echo or is there a better way to delimit separate commands?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

如梦 2024-10-10 14:05:11

这无法按照您在此处尝试的方式工作,因为环境变量是在解析一行时扩展的,而不是在执行时扩展的。由于 %MESSAGE% 仅在执行第一部分之后才有意义,因此这是行不通的。

然而,这将:

setlocal enabledelayedexpansion
set /p MESSAGE= && echo !MESSAGE!

请参阅help set以获取延迟扩展的讨论。

This cannot work the way you try here since environment variables are expanded while parsing a line, not when executing it. Since %MESSAGE% is only meaningful after executing the first part, this cannot work.

This however will:

setlocal enabledelayedexpansion
set /p MESSAGE= && echo !MESSAGE!

See help set for a discussion of delayed expansion.

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