Set environment variable outside bash

I am trying to set a bash environment variable using PHP (from the command line) without success.

$buff=array(); $buff[]="VARTESTKEY=VARTESTVALUE"; $buff[]="export VARTESTKEY"; file_put_contents('script.sh', implode("\n",$buff)); system('source script.sh'); 

I even tried using a script to output a key value that gets the value:

 $buff=array(); $buff[]="echo VARTESTKEY=VARTESTVALUE"; file_put_contents('script.sh', implode("\n",$buff)); system('eval "$(bash script.sh)"'); 

But still nothing.

Any ideas? I do not mind using any other tool (perl, python, c, etc.) if it can do its job by calling from a PHP system function.

+4
source share
1 answer

Do you need these environment variables before running another bash script?

You can simply use putenv("KEY=VAL");

Es:

 <?php putenv("ASD=LOL"); system("echo \$ASD"); ?> 

Edit:

 <?php echo "VARTESTKEY=VARTESTVALUE"; ?> 

run it like:

 $ eval `PHP .php` && echo $VARTESTKEY 
+4
source

Source: https://habr.com/ru/post/1336310/


All Articles