Export a variable from PHP to shell Export a variable from PHP to shell shell shell

Export a variable from PHP to shell


If you're trying to pass some output to a shell variable, you can do it like this:

$ testvar=$(php -r 'print "hello"')$ echo $testvarhello

Showing how export affects things:

$ php -r '$a=getenv("testvar"); print $a;'$ export testvar$ php -r '$a=getenv("testvar"); print $a;'hello

In these examples, the interactive shell is the parent process and everything else shown is a child (and siblings of each other).


Environment variables that are exported are only available in child processes.

So you'll be able to set an environment variable and then spawn a child process. The environment variable will be visible in that child process. However setting it in php and then launching a successive process (echo, in your example above) won't work.

If you set the variable and then spawn/exec a new process, it should be visible in that new process.