jcubic
jcubic

Reputation: 66650

Why I'm getting "sh" calling "echo $0" inside bash inside php exec?

I have code like this:

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

and when I call shell("echo $0"); I'm getting sh instead of bash, why?

Upvotes: 1

Views: 136

Answers (1)

Barmar
Barmar

Reputation: 782508

The original shell is expanding the variable inside doublequotes. To prevent variable expansion, use single quotes:

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

Upvotes: 3

Related Questions