简体   繁体   English

为什么有时在 PowerShell 的“python -c [command]”(以及命令提示符)中使用单引号将命令括起来时不起作用?

[英]Why doesn't it work sometimes when using single quotes to surround the command in `python -c [command]` in PowerShell (and in command prompt as well)?

In the Python docs it says:Python 文档中它说:

A second way of starting the interpreter is python -c *command* [arg]... , which executes the statement(s) in command, analogous to the shell's -c option.启动解释器的第二种方法是python -c *command* [arg]... ,它执行命令中的语句,类似于 shell 的-c选项。 Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote command in its entirety with single quotes.由于 Python 语句通常包含 shell 特有的空格或其他字符,因此通常建议使用单引号将整个命令引用。

So to test this feature, I tried this in PowerShell:所以为了测试这个特性,我在 PowerShell 中尝试了这个:

> py -c 'print("Hello, World!")'

and it shows this error:它显示了这个错误:

File "<string>", line 1
    print(Hello, World!)

However when I use double quotes instead it works just fine:但是,当我使用双引号时,它工作得很好:

> py -c "print('Hello, World!')"

It seems it only happens for strings.似乎它只发生在字符串上。 For numbers it works fine.对于数字,它工作正常。 I think the issue here has something to do with using quotes.我认为这里的问题与使用引号有关。 Why does it happen?为什么会这样?

my guess is (just like in powershell) single quotes are not interpreted but strings which are strings.我的猜测是(就像在 powershell 中一样)单引号不会被解释,而是字符串是字符串。 this means that python treats the first test py -c 'print("Hello, World!")' as all strings but py -c expects a command.这意味着 python 将第一个测试py -c 'print("Hello, World!")'视为所有字符串,但py -c需要一个命令。 whereas with double quotes python looks for functions (in your test print() ) within the string and executes them.而使用双引号 python 在字符串中查找函数(在您的测试print()中)并执行它们。

I did not find proof for this on the web but we have the same in powershell as this blog post describes我没有在 web 上找到这方面的证据,但我们在 powershell 中也有相同的证据,正如 这篇博文所述

Just stumbled upon this question and this awesome answer只是偶然发现了这个问题这个很棒的答案

A short summary:一个简短的总结:

  • It has nothing to do with python -c .它与python -c无关。 The fault's all PowerShell's.故障都是 PowerShell 的。
  • It's a known thing and has being there for years.这是众所周知的事情,并且已经存在多年。
  • PowerShell does a lot more parsing and escaping on the command you hand it, even if you told it to treat arguments literally. PowerShell 会根据您提交的命令进行更多的解析和 escaping,即使您告诉它按字面意思对待 arguments。 And when calling a native command (in contrast to a PowerShell cmdlet, python in this case), a single string CommandLine has to be contrived by PowerShell to invoke the underlying Win32 CreateProcess function. And when calling a native command (in contrast to a PowerShell cmdlet, python in this case), a single string CommandLine has to be contrived by PowerShell to invoke the underlying Win32 CreateProcess function.
  • PowerShell's counter-intuitive escaping approach when creating that single-string CommandLine will strip double quotes from command line arguments, properly escaped or not. PowerShell 在创建单字符串CommandLine时违反直觉的 escaping 方法将从命令行 arguments 中去除双引号,无论是否正确转义。

To bypass this feature, quote with single quotes or escape all double quotes with backslashes ( \" ). (Not being a valid escaping syntax in PS, \" will be ignored by PowerShell but correctly interpreted by Win32 function CommandLineToArgvW when building argv array from the single-string command line that the Windows process gets passed, as documented here .) To bypass this feature, quote with single quotes or escape all double quotes with backslashes ( \" ). (Not being a valid escaping syntax in PS, \" will be ignored by PowerShell but correctly interpreted by Win32 function CommandLineToArgvW when building argv array from Windows 进程通过的单字符串命令行,如此所述。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM