为什么CMD逃脱角色会变魔术而不是逃脱换行符 [英] Why CMD escape character does magic instead of escaping new line

查看:50
本文介绍了为什么CMD逃脱角色会变魔术而不是逃脱换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试执行简单的脚本:

I try to execute simple script:

set list="a",^
"b",^
c

echo %list%

输出为

"a","b",a","b",^

尽管我希望它是"a","b",c.引号似乎破坏了所有内容,因为如果b没有它们,则一切正常.对于我来说,也很神秘,为什么它仅在第二次换行时才中断.

Although I expected it to be "a", "b", c. It seems that the quotation marks spoil everything because if b is without them, all works fine. It is also mysterious to me why it breaks only on second new line escape.

那么,为什么输出如此奇怪?我尝试在Windows 7上启动脚本,

So, why the output is so strange? I tried to launch the script on Windows 7 if it matters

推荐答案

有趣的问题!看来,当 ^ 放在行尾时,不会解析下一行的第一个字符,而是直接插入结果而没有由解析器处理.如果必须平衡此类字符(如引号或括号),则会导致奇怪的错误",否则将与不平衡字符"相关联.

Interesting question! It seems that when the ^ is placed at end of line, the first character in next line is not parsed, but literally inserted in the result without being processed by the parser. If such character must be balanced (like quotes or parentheses) then this cause strange "errors" that otherwise would be associated to "unbalanced characters".

如果您更改第一个字符.在 ^ 之后的行中,问题消失了.例如,插入一个空格:

If you change the first char. in the line after ^, the problem disappear. For example, inserting a space:

set list="a",^
 "b",^
 c

...或移动逗号:

set list="a"^
,"b"^
,c

...或只是删除引号(如您已经测试过的那样).

... or just deleting the quote (as you already tested).

这篇关于为什么CMD逃脱角色会变魔术而不是逃脱换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆