How to create and delete environment variables in Bash
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 variableexport variable_name# delete environment variableunset variable_name
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 variablesecho "-> Displaying all environment variables before creating a new environment variable <-"env# create environment variableexport PROD_URL="https://educative.io"# display the environment variablesecho -e "\n-> Displaying updated environment variables after creating a new environment variable <-"env# delete environment variableunset PROD_URL# display the environment variablesecho -e "\n-> Displaying environment variables after deleting the new environment variable <-"env
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_URLusing the commandexport.Lines 9–10: We display the environment variables with the command
env. We can see that there's a new variablePROD_URL=https://educative.ioin the code output.Line 13: We delete the environment variable
PROD_URLusing the commandunset.Lines 16–17: We display the environment variables with the command
env.
Free Resources