Introduction
In Linux systems, environment variables are essential components that store data which can be used by various programs and processes. These variables define system configuration, control program behavior, and store temporary data. As a system administrator or developer, knowing how to manage these variables efficiently is crucial for maintaining a clean and well-organized operating environment.
This lab focuses on the important skill of unsetting environment variables in Linux. You will learn how to identify existing variables, temporarily remove them using the unset command, and permanently remove them by modifying configuration files. These skills are fundamental for system administration tasks, debugging environment issues, and managing application configurations.
Understanding Environment Variables
Environment variables are named values that can affect how processes run on a computer. In this step, you will learn how to view environment variables in your Linux system.
Listing All Environment Variables
First, let's see all currently set environment variables. Open your terminal and execute the following command:
printenv
This command displays all environment variables currently set in your session. You will see a long list of variables with their values. Each line represents one variable in the format VARIABLE_NAME=value.
Checking a Specific Environment Variable
To view the value of a specific environment variable, you can use the printenv command followed by the variable name. Let's check the value of the OLD_TRADE_DATA variable that has been set up for this lab:
printenv OLD_TRADE_DATA
You should see the following output:
Outdated Info
This indicates that the variable OLD_TRADE_DATA is set to the value "Outdated Info".
Another way to view the value of an environment variable is using the echo command with the $ prefix:
echo $OLD_TRADE_DATA
This should also display:
Outdated Info
Now that you understand how to view environment variables, you are ready to learn how to unset them in the next step.
Temporarily Unsetting Environment Variables
In this step, you will learn how to temporarily unset an environment variable for your current session. When you temporarily unset a variable, it will only be removed for the current terminal session. If you open a new terminal or log in again, the variable will reappear if it's defined in a configuration file.
Using the unset Command
The unset command is used to remove a variable from the current environment. Let's use it to remove the OLD_TRADE_DATA variable:
unset OLD_TRADE_DATA
This command doesn't produce any output if successful. To verify that the variable has been unset, check its value again:
printenv OLD_TRADE_DATA
You should notice that there is no output now, which means the variable has been successfully unset.
You can also verify with the echo command:
echo $OLD_TRADE_DATA
Again, there should be no output, just an empty line, confirming that the variable is no longer set in your current session.
Understanding Temporary Nature
It's important to understand that using the unset command only affects the current terminal session. If the variable is defined in a configuration file like .zshrc, it will be set again when you start a new terminal session or reload the configuration file.
To test this, let's open a new terminal window or tab by clicking on the terminal menu and selecting "New Terminal". In the new terminal, check if the variable exists:
printenv OLD_TRADE_DATA
You should see the value "Outdated Info" again, because the variable is defined in your .zshrc file and is reloaded when you start a new terminal session.
Return to your original terminal window to continue with the next step, where you'll learn how to permanently unset environment variables.
Permanently Unsetting Environment Variables
In the previous step, you learned how to temporarily unset an environment variable for your current session. Now, you will learn how to permanently unset a variable by modifying the configuration files where it is defined.
Locating the Variable Definition
First, let's find where the OLD_TRADE_DATA variable is defined. In this lab, it has been added to your .zshrc file, which is located in your home directory. Let's view this file:
nano ~/.zshrc
This opens the .zshrc file in the nano text editor. Scroll through the file and look for a line that contains OLD_TRADE_DATA. You should find a line like this:
export OLD_TRADE_DATA="Outdated Info"
Modifying the Configuration File
To permanently unset the variable, you have two options:
Comment out the line by adding a
#at the beginning:#export OLD_TRADE_DATA="Outdated Info"Delete the line completely by positioning your cursor at the beginning of the line and pressing
Ctrl+Kto cut the entire line.
For this lab, let's use the first approach. Add a # at the beginning of the line to comment it out.
After making the changes, save the file by pressing Ctrl+O, then Enter, and exit nano by pressing Ctrl+X.
Applying the Changes
After modifying the configuration file, you need to reload it for the changes to take effect in your current session. Use the source command:
source ~/.zshrc
Verifying the Changes
To verify that the variable is permanently unset, check its value again:
printenv OLD_TRADE_DATA
There should be no output, indicating that the variable is no longer set.
To further verify that the changes will persist across new terminal sessions, you can open a new terminal window or tab and check the variable:
printenv OLD_TRADE_DATA
Again, there should be no output, confirming that the variable is permanently unset.
Congratulations! You have successfully learned how to permanently unset environment variables in Linux by modifying configuration files.
Summary
In this lab, you have learned essential skills for managing environment variables in Linux systems:
Understanding Environment Variables: You learned how to view all environment variables using the
printenvcommand and how to check the value of a specific variable using bothprintenv VARIABLE_NAMEandecho $VARIABLE_NAME.Temporarily Unsetting Variables: You used the
unsetcommand to remove a variable from your current terminal session, understanding that this change only affects the current session.Permanently Unsetting Variables: You learned how to make permanent changes by modifying configuration files like
.zshrc, either by commenting out or removing the variable definition, and then applying these changes with thesourcecommand.
These skills are valuable for system administration, software development, and debugging environment-related issues in Linux systems. Proper management of environment variables helps maintain a clean and well-organized system environment, preventing conflicts and ensuring that applications have access to the correct configuration settings.



