Dmitrii
Dmitrii

Reputation: 3557

Can't get new environment variable via getenv

I have a linux VM (Linux Lite 4) running PHP. I'm adding a custom environment variable in terminal with

export MYVAR=bar

and can see it locally afterward with echo $MYVAR returning bar.

The problem is: when I connect to this server over ssh and try to query the variable by running a simple PHP script located on a server

<?php
var_dump(getenv('MYVAR'));

I'm getting bool(false). Other default env variables are queried successfully.

Feel free to correct me if I'm doing something wrong.

Upvotes: 0

Views: 1499

Answers (1)

Kurtis Rader
Kurtis Rader

Reputation: 7459

Env vars in UNIX are not global. They are private to each process. When a process, such as your interactive shell, starts a new process it typically gives the new process a copy of its env vars. So when you define a new env var in an interactive shell it is not visible to already running processes such as your web server. Nor is it visible to processes, such as your php program, that are started independent of the shell where you defined the env var.

Without knowing a lot more about your configuration it isn't possible to provide good advice. But given what you wrote it may be sufficient to put the export MYVAR=bar in your ~/.bashrc (assuming bash is your login shell).

Upvotes: 3

Related Questions