title: How to access PHP environment variables in a bash script?
body: <p>I'm writing a bash script meant to run on a remote AMP stack. The script needs to access a PHP predefined environment variable ($_ENV). </p>
  
  <p>This is what I want:</p>
  
  <pre><code>db_host=$(php -r 'echo $_ENV{DATABASE_SERVER};')
  echo "The DB Host is: $db_host"
  # output: "The DB Host is: internal-db.s173785.gridserver.com"
  </code></pre>
  
  <p>This is what I get instead:</p>
  
  <pre><code># METHOD 1
  db_host1=$(php -r 'echo $_ENV{DATABASE_SERVER};')
  echo "The DB Host is: $db_host1"
  # output: "The DB Host is: "
  
  # METHOD 2
  db_host2=`php -r 'echo get_env(DATABASE_SERVER);'`
  echo "The DB Host is: $db_host2"
  # output: "The DB Host is: "
  </code></pre>
  
  <p>Neither method works, both variables return empty. I know that this PHP variable is set, because when I type this into the terminal (after ssh'ing into the server), I get the expected value:</p>
  
  <pre><code>$ php -r 'echo $_ENV{DATABASE_SERVER};' 
  # outputs: "internal-db.s173785.gridserver.com"
  </code></pre>
  
  <p>Technically the above methods should work, because I managed to get this working in my script:</p>
  
  <pre><code>php_user=$(php -r 'echo getenv("USER");')
  echo php_user is $php_user
  # outputs: "php_user is myusername"
  </code></pre>
  
  <p>Anyone know what I am doing wrong?\</p>
  
  <p><strong>***UPDATE*******</strong></p>
  
  <p>I should mention that I am invoking this script from my local machine like so:     </p>
  
  <pre><code>ssh -t user@mydomain.com "myscript backup_remote_db"
  </code></pre>
  
  <p>"myscript" is the name of my executable bash script, and "backup_remote_db" is the function I'm passing to it which contains the code above.</p>
  
  <p>This might not be cause however, because when I echo $USER in the script, it echoes the remote user, not the local one...</p>
  
  <p><strong>***UPDATE 2******</strong></p>
  
  <p>Here is how I finally got it working:</p>
  
  <pre><code>db_host=$DATABASE_SERVER
  echo "The DB Host is $db_host"
  # output: "The DB Host is: internal-db.s173785.gridserver.com"
  </code></pre>
  
  <p>But only if I make this adjustment to how the script is invoked:</p>
  
  <pre><code>ssh -t user@mydomain.com ". /etc/profile; myscript backup_remote_db"
  </code></pre>