Search code examples
command-lineenvironment-variablesexecphing

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

Solution

  • 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.