Export environment variable from PHP, for use by exec()'d shell command?

Posted by anonymous coward on Stack Overflow See other posts from Stack Overflow or by anonymous coward
Published on 2010-04-21T21:17:52Z Indexed on 2010/04/21 21:33 UTC
Read the original article Hit count: 178

Filed under:
|
|

Somewhat similar to http://stackoverflow.com/questions/2002970/export-a-variable-from-php-to-shell, but my problem is different.

I am forced to work with a program that gets an important location variable from an Environment Variable (Linux), rather than a config or, better, a command line option.

I have to call this program from within PHP, and currently am doing it like so:

shell_exec('/path/to/program option1 option2'); 

I need to be able to set/override the environment variable that program will be looking for. As an example, this does not work:

shell_exec('VAR1=foo /path/to/program option1 option2'); 

nor does:

shell_exec('VAR1=foo; export foo; /path/to/program option1 option2'); 

for good measure.

Both of the above command lines work "as expected" from the shell.

Is it possible to override environment variables for programs called via exec, shell_exec, etc?

© Stack Overflow or respective owner

Related posts about php

Related posts about shell