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.
# 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.
# 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
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
.