从命令行运行 PHP 是否需要 #!/usr/bin/env ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8737898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is #!/usr/bin/env required to run PHP from command line?
提问by JasonDavis
Often times when I see PHP that is meant to be ran from the command line, it will have this line #!/usr/bin/env php
at the top of the file like this...
很多时候,当我看到要从命令行运行的 PHP 时,它会#!/usr/bin/env php
像这样在文件顶部有这一行...
#!/usr/bin/env php
<?php
// code
?>
I was wanting to know if this is meant just for when the file is ran on a Linux/Unix system or is needed for running on Windows as well?
我想知道这是否仅适用于文件在 Linux/Unix 系统上运行的情况,还是也需要在 Windows 上运行?
回答by Patrick Fisher
The "hashbang" line is required for auto-detection of the type of script. It enables this sort of usage:
自动检测脚本类型需要“hashbang”行。它启用了这种用法:
[pfisher ~]$ chmod +x run-me.php
[pfisher ~]$ run-me.php
That line is not needed if you pass the filename as an argument to the php interpreter, like so:
如果您将文件名作为参数传递给 php 解释器,则不需要该行,如下所示:
[pfisher ~]$ php run-me.php
回答by Ahmed Masud
No it's not, you can directly use
不,不是,你可以直接使用
#!/path/to/php
Running php (or anything else) through the env utility is a weak security measure. Dpending on the platform, will "fix" PATH, LIB, and other environment variables according to various config files and potentially remove some of the dangerous values in there (e.g. env on HPUX).
通过 env 实用程序运行 php(或其他任何东西)是一种弱安全措施。取决于平台,将根据各种配置文件“修复”PATH、LIB 和其他环境变量,并可能删除其中的一些危险值(例如 HPUX 上的 env)。
It is also to limit the scope of shell-expansions on certain environments. (See man 1 env on Linux).
这也是为了限制某些环境中的 shell 扩展的范围。(请参阅 Linux 上的 man 1 env)。