Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phing exec command to set environment variable

I'm trying to set an environment variable in a build script with phing. This is normally done command line like this:

export MY_VAR=value

In Phing I did the following but it isn't working.

<exec command="export MY_VAR=value" />
like image 313
tom Avatar asked Apr 27 '11 13:04

tom


2 Answers

I see that this is quite an old question, but I don't think it has been answered in the best way. If you wish to export a shell variable, for example say you are running phpunit from phing and want to do an export before invoking phpunit, try:

<exec command="export MY_VAR=value ; /path/to/phpunit" />

Simply do the export and invoke your command inside the same exec tag. Separate the export statement and the shell executable with a semicolon as shown. Your script will be able to access the value using the standard php function:

$myVar = getenv('MY_VAR');
like image 97
Andy H Avatar answered Sep 27 '22 18:09

Andy H


Bold claim: There is no way to set/export a (Unix) shell variable in PHP so that it is visible inside the scope that started the php script.

php myfile.php (does putenv or shell_exec('export foo=bar');)
echo $foo

Will return nothing. As PHP can not do it so neither can phing.

Accessing shell environment variables accross multiple script runs (if its that what you want) seems also like an unideal design decision, pretty stateful.

Apart from that I'd urge you to stick to phing and learn its lean lesson. Phing helps stateless thinking to some degree.

like image 22
Christoph Strasen Avatar answered Sep 27 '22 18:09

Christoph Strasen