Export a variable from PHP to shell

后端 未结 2 687
野趣味
野趣味 2021-01-13 08:10

I\'m trying to set a variable that should be accessible from outside PHP. Ideally this should be a local variable, but environment variables are also welcome.

First,

2条回答
  •  时光说笑
    2021-01-13 08:42

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

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

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

提交回复
热议问题