Phing exec command to set environment variable

tom picture tom · Apr 27, 2011 · Viewed 7.4k times · Source

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" />

Answer

Andy H picture Andy H · Jul 15, 2014

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');