export shell environment variable before running command from PHP CLI script

◇◆丶佛笑我妖孽 提交于 2019-12-05 04:45:05

I'm not 100% familiar with how PHP's exec works, but have you tried: exec("LD_LIBRARY_PATH=/path/to/lib $cmd")

I know that this works in most shells but I'm not sure how PHP doing things.

EDIT: Assuming this is working, to deal with multiple variables just separate them by a space:

exec("VAR1=val1 VAR2=val2 LD_LIBRARY_PATH=/path/to/lib $cmd")

You could just prepend your variable assignments to $cmd.

[ghoti@pc ~]$ cat doit.php 
<?php

$cmd='echo "output=$FOO/$BAR"';

$cmd="FOO=bar;BAR=baz;" . $cmd;

print ">> $cmd\n";
passthru($cmd);

[ghoti@pc ~]$ php doit.php 
>> FOO=bar;BAR=baz;echo "output=$FOO/$BAR"
output=bar/baz
[ghoti@pc ~]$ 

A couple things come to mind. One is for $cmd to be a script that includes the environment variable setup before running the actual program.

Another thought is this: I know you can define a variable and run a program on the same line, such as:

DISPLAY=host:0.0 xclock

but I don't know if that work in the context of passthru

https://help.ubuntu.com/community/EnvironmentVariables#Bash.27s_quick_assignment_and_inheritance_trick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!