Environment variables are variables that are defined in the system environment. They are a great way to have environment-based configurations passed into our application.
To get the value of an environment variable on Linux/Unix systems, we can do the following:
echo $PATH
Each process has its own set of environment variables. A child process inherits a copy of the parent’s environment.
echo $PWDexport PWD="/Educative"echo $PWD
Following are some common environment variables:
$PATH
$HOME
$PWD
Most modern-day programming languages allow us to access the environment they are running in. They make it easy to keep the configuration and application loosely coupled. We can make changes to the execution environment without any code changes.
For example, we can set the dev
environment variable to be different from the test environment. The test environment could be different from our prod environment.
We want the application running on the dev
environment to connect with the dev database, test
environment with the test database, and prod
environment with the prod database.
Let’s look at how we can set up dev
environment:
export DB="dev.db.com"echo $DB
Let’s look at how we can set up a test
environment:
export DB="test.db.com"echo $DB
Let’s look at how we can set up a prod
environment:
export DB="prod.db.com"echo $DB