【问题标题】:Why I'm getting "sh" calling "echo $0" inside bash inside php exec?为什么我在 php exec 的 bash 中调用“echo $0”的“sh”?
【发布时间】:2014-03-17 07:54:19
【问题描述】:

我有这样的代码:

public function shell($code) {
    $code = preg_replace('/"/', '\\"', $code);
    exec('bash -c "' . $code . '"', $result);
    return $result;
}

当我打电话给shell("echo $0"); 时,我得到的是 sh 而不是 bash,为什么?

【问题讨论】:

    标签: php linux bash shell exec


    【解决方案1】:

    原来的 shell 扩展了双引号内的变量。为防止变量扩展,请使用单引号:

    exec("bash -c '" . $code . "'", $result);
    

    【讨论】:

      猜你喜欢
      • 2012-01-17
      • 2018-09-26
      • 2015-07-22
      • 1970-01-01
      • 2021-02-22
      • 2013-02-17
      相关资源
      最近更新 更多