Trusted answers to developer questions

What is an environment variable?

Get the Learn to Code Starter Pack

Break into tech with the logic & computer science skills you’d learn in a bootcamp or university — at a fraction of the cost. Educative's hand-on curriculum is perfect for new learners hoping to launch a career.

Overview

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.

Syntax

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.

Example

echo $PWD
export 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

RELATED TAGS

system
operating system
Did you find this helpful?