交互式命令提示符字符转义%

发布于 2025-01-14 09:07:07 字数 242 浏览 4 评论 0原文

我需要在交互式命令提示符中转义 % 字符(不在批处理文件中)。如何回显文本 a %pathext% b; %pathext% 扩展为环境变量; ^%\"quotes" 均无效。答案请基于:echo a %pathext% b

非常感谢。

I need to escape the % character in interactive Command Prompt (NOT in a batch file). How do I echo the text a %pathext% b; %pathext% expands to the environment variable; ^, %, \ and "quotes" all do not work. Please base the answer on: echo a %pathext% b.

Many thanks in advance.

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

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

发布评论

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

评论(2

寄居人 2025-01-21 09:07:07

有一个简单的解决方案,但它不是 100% 防弹的

echo a %path^ext% b

插入符号的位置可以移动到任何位置。
这是可行的,因为命令行上的变量扩展与批处理文件中的变量扩展不同。
如果应扩展未定义的变量,则在批处理文件中会生成空文本,但在 cli 上,百分比表达式将保持不变。
插入符号将在解析器的后续步骤中被删除。

但是,当存在名为 path^ext 的变量时,它仍然可能会失败。

对于防弹解决方案,您需要创建一个百分号并将其展开。

for /F "delims==" %# in ("%=%=") do echo a %#pathext%# b

There is one simple solution, but it's not 100% bullet proof

echo a %path^ext% b

The position of the caret can be moved to any position.
This works, because the variable expansion on the command line works different than in batch files.
If an undefined variable should be expanded, in a batch file it results in an empty text, but on the cli the percent expression will be used unchaged.
The caret will be removed in a later step of the parser.

But it can still fail when there exists a variable named path^ext

For a bullet proof solution, you need to create a percent sign and expand it.

for /F "delims==" %# in ("%=%=") do echo a %#pathext%# b
伊面 2025-01-21 09:07:07
echo a %^pathext% b

在变量内使用插入符号(可以在 % 符号内的任何位置)以防止解析器识别和替换该变量。

echo a %^pathext% b

Use a caret inside the variable (it can be anywhere inside the % signs) to prevent the parser from recognizing and substituting the variable.

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