从PHP中使用的bash shell [英] Using bash shell from within PHP
问题描述
在我的终端窗口(使用最大OS X)我的shell是bash。然而,当我通过了shell_exec或反引号操作符运行PHP命令我看到PHP使用的是Bourne shell(SH)。下面是我看到的例子:
In my terminal window (using Max OS X) my shell is bash. However when I run a command in PHP via shell_exec or backtick operators I see that PHP is using the Bourne Shell (sh). Here's an example of what I'm seeing:
从我的终端窗口中:
$ echo $0
- bash
另外,如果我称之为PHP如下:
Also if I call php as follows:
$ php -r "echo shell_exec('echo $0');"
-bash
不过,如果我创建一个名为test.php的与下面的脚本:
However, if I create a script called test.php with the following:
<?php echo shell_exec('echo $0'); ?>
然后运行测试PHP我得到如下:
And then run test php I get the following:
$ php test.php
sh
我想使用bash shell中调用了shell_exec时 - ?它为什么选择Bourne shell和我可以迫使它使用bash
I'm wanting to use the bash shell when calling shell_exec - why is it choosing the Bourne shell and can I force it to use bash?
谢谢!
丹
推荐答案
反转行情在你的第二个命令:
Reverse the quotes in your second command:
$ php -r 'echo shell_exec("echo $0");'
sh
随着你在你的问题有他们的报价,变量 $ 1,0
命令前扩展发送到 PHP
。
如果您想强制使用bash,你可以这样做:
If you want to force the use of Bash, you could do something like:
php -r '$cmd="echo \\\$0"; echo shell_exec("/bin/bash -c \"$cmd\"");'
这篇关于从PHP中使用的bash shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!