微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Windows中转义空间

我在Linux环境中有很多的经验,但现在我需要在Windows中做一些事情。 我有一些麻烦,搞清楚如何正确地转义Windows命令环境中的空间。 关于如何parsing命令的规则完全不清楚。

例如,在Linux环境中,我可以创build以下脚本:

我的command.sh

#!/bin/bash echo "Called as '$0' with argument '$1'"

并像这样执行它,所有可能的逃避空间的方法是等价的。

命令提示符:当我将一个文件夹(包含内容)从桌面移动到一个新的目录时,为什么会出现“找不到指定的path”?

Windows命令提示符(cmd)移动命令

Windows cmd中CD / D开关的原因是什么?

Windows命令提示符将错误logging到文件

如何在命令提示符下创build特定命令的快捷方式?

[bradworkubuntu:~/command_test] my command.sh arg with spaces Called as './my command.sh' with argument 'arg with spaces' [bradworkubuntu:~/command_test] "my command.sh" arg with spaces Called as './my command.sh' with argument 'arg with spaces' [bradworkubuntu:~/command_test] my command.sh "arg with spaces" Called as './my command.sh' with argument 'arg with spaces' [bradworkubuntu:~/command_test] "my command.sh" "arg with spaces" Called as './my command.sh' with argument 'arg with spaces' [bradworkubuntu:~/command_test] /home/brichardson/command_test/my command.sh arg with spaces Called as '/home/brichardson/command_test/my command.sh' with argument 'arg with spaces' [bradworkubuntu:~/command_test] "/home/brichardson/command_test/my command.sh" arg with spaces Called as '/home/brichardson/command_test/my command.sh' with argument 'arg with spaces' [bradworkubuntu:~/command_test] "/home/brichardson/command_test/my command.sh" "arg with spaces" Called as '/home/brichardson/command_test/my command.sh' with argument 'arg with spaces'

但是当我在Windows上尝试类似的时候,结果是完全不一致的。

我的command.bat

echo "Called as '%0' with argument '%1'"

执行如下:

C:UsersbrichardsonDocumentscommand_test>my^ command.bat arg^ with^ spaces 'my' is not recognized as an internal or external command,operable program or batch file. C:UsersbrichardsonDocumentscommand_test>"my command.bat" arg^ with^ spaces C:UsersbrichardsonDocumentscommand_test>echo "Called as '"my command.bat"' with argument 'arg'" "Called as '"my command.bat"' with argument 'arg'" C:UsersbrichardsonDocumentscommand_test>my^ command.bat "arg with spaces" 'my' is not recognized as an internal or external command,operable program or batch file. C:UsersbrichardsonDocumentscommand_test>"my command.bat" "arg with spaces" C:UsersbrichardsonDocumentscommand_test>echo "Called as '"my command.bat"' with argument '"arg w ith spaces"'" "Called as '"my command.bat"' with argument '"arg with spaces"'" C:UsersbrichardsonDocumentscommand_test>UsersbrichardsonDocumentscommand_testmy^ command.ba t arg^ with^ spaces 'UsersbrichardsonDocumentscommand_testmy' is not recognized as an internal or external command,operable program or batch file. C:UsersbrichardsonDocumentscommand_test>"UsersbrichardsonDocumentscommand_testmy command.ba t" arg^ with^ spaces C:UsersbrichardsonDocumentscommand_test>echo "Called as '"UsersbrichardsonDocumentscommand_t estmy command.bat"' with argument 'arg'" "Called as '"UsersbrichardsonDocumentscommand_testmy command.bat"' with argument 'arg'" C:UsersbrichardsonDocumentscommand_test>"UsersbrichardsonDocumentscommand_testmy command.ba t" "arg with spaces" C:UsersbrichardsonDocumentscommand_test>echo "Called as '"UsersbrichardsonDocumentscommand_t estmy command.bat"' with argument '"arg with spaces"'" "Called as '"UsersbrichardsonDocumentscommand_testmy command.bat"' with argument '"arg with spa ces"'" C:UsersbrichardsonDocumentscommand_test>C:UsersbrichardsonDocumentscommand_testmy^ command. bat arg^ with^ spaces C:UsersbrichardsonDocumentscommand_test>echo "Called as 'C:UsersbrichardsonDocumentscommand_ testmy command.bat' with argument 'arg'" "Called as 'C:UsersbrichardsonDocumentscommand_testmy command.bat' with argument 'arg'" C:UsersbrichardsonDocumentscommand_test>C:UsersbrichardsonDocumentscommand_testmy^ command. bat "arg with spaces" C:UsersbrichardsonDocumentscommand_test>echo "Called as 'C:UsersbrichardsonDocumentscommand_ testmy command.bat' with argument '"arg with spaces"'" "Called as 'C:UsersbrichardsonDocumentscommand_testmy command.bat' with argument '"arg with spa ces"'" C:UsersbrichardsonDocumentscommand_test>"C:UsersbrichardsonDocumentscommand_testmy command. bat" "arg with spaces" C:UsersbrichardsonDocumentscommand_test>echo "Called as '"C:UsersbrichardsonDocumentscommand _testmy command.bat"' with argument '"arg with spaces"'" "Called as '"C:UsersbrichardsonDocumentscommand_testmy command.bat"' with argument '"arg with s paces"'"

任何人都可以向我解释当Windows命令提示符将或不会逃脱空格? 而当它存在或不存在时,它将在环境中实际存储什么?

据我所知,它只能在命令path中跳过空格,并且只有在包含驱动器号的情况下才会出现。 如果你使用引号来处理空格,它将把它们包含在传递给命令的任何string中。

在Windows命令提示符下使用'python -c'选项

Cmd提示与Ubuntuterminal相同吗?

代码中使用disM

使用Windows命令提示重命名多个文件

提高Windows的批处理脚本性能

要理解的最重要的事情是有两个独立的解析阶段发生。 第一阶段是寻找特殊字符的命令解释器,例如输入重定向。 第二阶段是可执行文件解析命令行中剩下的任何单独的参数。

在这种情况下,您正在运行一个批处理文件,所以解析的第二个阶段由批处理器处理。 (注意批处理器实际上并不是一个单独的命令解释器进程,但是对于我们的目的来说,它们最好被认为是概念上不同的)。

所以,如果你开始

myapp.exe two^ is ^> one,"I think" > test.txt

命令解释器重定向输出并将此命令行传递给可执行文件

myapp.exe two is > one,"I think"

如果可执行文件使用C运行时提供的认解析,那么它将成为一个数组

"myapp.exe","two","is",">","one,","I think"

(关于这个问题的答案,关于命令行是如何处理的,会有更多的细节。)

这与你的问题有关的第一个原因是因为^在解析的第一阶段只是一个特殊的字符。 因此,它会转义特殊字符,如> <和| 但是对命令行被分割成单独的参数的方式没有影响。 这解释了许多测试用例的行为。

第二个相关的原因是第二阶段的解析规则完全取决于可执行文件,或者在这种情况下是批处理器来决定。 碰巧,批处理器可以选择在参数包含空格时保留引号。 (这大概是这样,以便包含空格的文件名将被正确处理;大多数批处理文件参数是文件名。

注意:如果您不想使用引号,请使用%~1而不是%1 。 这是在for命令的上下文中记录的for但也适用于命令行参数。

还应该注意,由C运行时库提供的认解析不包括作为参数一部分的引号。

…我认为这涵盖了一切,除…

这个观察

c:pathmy^ command

运行my command而不是my对我感到惊讶。 我无法解释这一点。 如果my command是一个基于C的可执行文件,这是一个特别糟糕的消息,因为“command”将会翻倍,也就是说,可执行文件会将其视为第一个参数。

我认为这是一个错误。 我建议你避免这样做。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐