![]() |
A variable is the basic building block that stores a value in a specific memory location, and any operation performed on the variable affects that memory location. Values stored in variables can be changed or deleted during program execution. Environment variables are the variables whose value is set globally in the program and can be inherited by the child shell. In this article, we will see the different methods that can be used to remove exported environment variables in the shell. Local vs. user vs. system-wide environment variables
Creating Variable With Export CommandUsing the Export command, we will first create an Environment variable. export message="Hello from GeeksforGeeks" Using the env command, we will check the Environment variable. env | grep message ![]() Export Command There are many ways to clear the exported environment variables in the shell, some of which are explained as follows: Method 1: Clear the value of an Exported Variable by assigning an empty value.Note: Using this method, we cannot delete the variable from the environment, but we can delete the value of the exported variable by assigning a null value. export message= Now using the env command we will check the message. env | grep message You can see that we have assigned the empty value to the message variable so we have got the empty message as an output. ![]() Assigning empty value Method 2: Delete an Exported Environment Variable in Shell Using Unset CommandThe Unset command is an in-built shell command that can be used to clear the value of an exported variable. Let’s use this command to remove an exported environment variable and verify that the exported variable was removed: unset message env | grep message By using the unset command we have deleted the message variable. ![]() Unset Command Method 3: Clear the value of an Exported Variable with the env CommandWith the help of the below env command we can clear the value of an exported variable: env -i bash In the above command, we have -i which will clear user-exported variables and start a new Bash session with default values. ![]() env Command To check that variable is present in the older bash session run the following command: exit env | grep message ![]() Checking Variable ConclusionIn summary, environment variables are fundamental for storing global values in a shell. We explored methods to manage them: assigning an empty value using ‘export‘, removing them with ‘unset’, and resetting with ‘env’. These techniques are vital for efficient shell scripting and system configuration, granting control over processes and applications in Unix-like environments. Mastering these methods empowers effective manipulation of environment variables for diverse needs in shell programming and system administration. |
Reffered: https://www.geeksforgeeks.org
Linux Unix |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |