0

Some PHP Code cannot get HOME environment variable.

getenv('HOME'); // OK

exec("env",$out);
print_r($out,1); // OK

exec("echo $HOME", $out);
print_r($out,1); // NG 
1
  • Exec probably doesn't set any environment variables. You could try exec("export", $out); print_r($out,1); to see which env vars are set. Note however, that using exec like you are doing won't work on Windows.
    – mhvelplund
    Sep 6, 2018 at 8:34

1 Answer 1

3

It's because your string is in double quotes and PHP is substituting the value of the variable $HOME. Try changing the string to use single quotes. I.e.

exec('echo $HOME', $out);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.