Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

bash
communitycreator
env

How to create and delete environment variables in Bash

Gutha Vamsi Krishna

Overview

An environment variable is a variable whose value is set outside of the program. In this shot, we will go through how to create and delete an environment variable using Bash.

We can create and delete the environment variables using the following syntax.

Syntax

# create environment variable
export variable_name

# delete environment variable
unset variable_name
Syntax to create and delete env variables in bash

The export keyword will create the environment variable, while the unset keyword will delete the environment variable.

Let's take a look at an example.

Code

# display the environment variables
echo "-> Displaying all environment variables before creating a new environment variable <-"
env

# create environment variable
export PROD_URL = "https://educative.io"

# display the environment variables
echo -e "\n-> Displaying updated environment variables after creating a new environment variable <-"
env

# delete environment variable
unset PROD_URL

# display the environment variables
echo -e "\n-> Displaying environment variables after deleting the new environment variable <-"
env
Creating and deleting a new env variable in Bash

Explanation

In the above code snippet, we see the following:

  • Lines 2–3: We print all of the environment variables using the command env.
  • Line 6: Now, we will create an environment variable PROD_URL using the command export.
  • Lines 9–10: We display the environment variables with the command env. We can see that there's a new variable PROD_URL=https://educative.io in the code output.
  • Line 13: We delete the environment variable PROD_URL using the command unset.
  • Lines 16–17: We display the environment variables with the command env.

      RELATED TAGS

      bash
      communitycreator
      env
      RELATED COURSES

      View all Courses

      Keep Exploring