FUNDAMENTALS A Complete Guide for Beginners
Environment variables play a crucial role in managing system configurations and influencing the behavior of programs in Unix-like operating systems. They hold dynamic values that can impact the execution of scripts, applications, and various system processes. While setting environment variables is common practice, there are situations where unsetting or removing these variables becomes equally important. In this article, I will discuss different methods to unset environment variable using bash script. So Let’s start!
2 Methods to Unset Environment Variable Using Bash Scripts
Unset environment variable is essential for effective configuration management. Let’s delve into the intricacies of unsetting variables, focusing on two distinct methods: the unset command and the set command with the -n flag.
Method 01: Using the “unset” Command to Remove Environment Variables
You can unset a shell variable or a function using the unset command followed by the variable or function name. Follow the two practical cases and understand the process fully.
Case 01: Unset Shell Environment Variables
To unset shell environment variables using the unset command, follow the steps below:
- At first, launch an Ubuntu Terminal.
- Write the following command to open a file in Nano:
nano unset_variable.sh
- Copy the script mentioned below:
#!/bin/bash # Setting a variable my_variable="Hello, World!" echo "Before unset: $my_variable" # Unsetting the variable unset my_variable echo "After unset: $my_variable" # This will output an empty line
EXPLANATIONThe Bash script initializes a variable named
my_variable
with the value “Hello, World!” and then displays its content using the echo command. The script demonstrates the variable’s content before unsetting it using theunset
command. After unsetting, attempting to display the variable again results in an empty line, showcasing that the variable has been removed from memory. - Press CTRL+O and ENTER to save the file; CTRL+X to exit.
- Use the following command to make the file executable:
chmod u+x unset_variable.sh
- Now, run the script by the following command:
./unset_variable.sh
The command line displays Before unset: Hello, World! first, however in the second line, After unset: it doesn’t return Hello, World! since the string value is already been removed.
Case 02: Unset the Environment Function
To unset shell environment function using the unset command, follow the steps below:
#!/bin/bash
# Define an environment function
my_function() {
echo "This is my function."
}
# Call the function
my_function
# Unset (remove) the function
unset -f my_function
# Try calling the function again (it will result in an error)
my_function
The Bash script defines an environment function called my_function using the function_name() { ... }
syntax. It then calls the function, which echoes “This is my function.” Next, the script removes the function using unset -f my_function
command. Lastly, the function my_function is called one more.
Upon calling the function for the first time, it prints “This is my function”. However, attempting to call the function again after unsetting, results in an error since the function has been removed from memory and can no longer be invoked.
Method 02: Using “set -n” Command to Remove Environment Variables
You can unset environment variables by utilizing the set command along with the -n option. Here’s how:
#!/bin/bash
# Set an environment variable
export MY_VARIABLE="Hello, World!"
echo "Before unset: $MY_VARIABLE"
# Unset the environment variable
set -n MY_VARIABLE
echo "After unset: $MY_VARIABLE" # This will output an empty line
The Bash script sets an environment variable named MY_VARIABLE
using the export
command, storing the value “Hello, World!“. The script then displays the variable’s value before unsetting it. Then it unset the environment variable using set -n MY_VARIABLE
. Finally, it displays the result using the echo
command.
As the image suggests, the bash script returns “Before unset: Hello, World!”. However, attempting to call the variable again after unsetting, results in no output in the command line since the variable has been removed from memory.
Conclusion
In this article, I have demonstrated 2 different methods to unset environment variables using bash scripts. By utilizing and understanding it, you can proficient your coding skill whenever you need to remove the environment variables. However, if you have any questions or queries, feel free to comment below. I will be one click away to answer your question. Thank You!
People Also Ask
What does unset environment variable mean?
Unsetting an environment variable means removing or deleting its value from the system, making it no longer accessible to processes and scripts that rely on it.
How do you unset an environment variable in batch?
To unset an environment variable in a Bash script (Unix-like systems), use the unset command followed by the variable name, like this: unset VARIABLE_NAME
Which command can be used to remove an environment variable?
The unset command can be used to remove an environment variable. Apart from that set command with the -n
option can also be used to accomplish the same task.
Related Articles
- How to Echo Variables in Bash Script? [4 Practical Examples]
- How to Use String Variables in Bash Script? [4 Cases]
- How to Append String to Bash Variable? [2 Effective Ways]
- How to Check If Bash Variable Exists? [2 Effective Methods]
- How To Check if Bash Variable is Empty? [2 Easy Methods]
- How to List and Set Bash Environment Variables? [3 Methods]
- 5 Methods to Check If Environment Variable is Set in Bash Script
- How to Set Bash $PATH Variable? [Easiest Configuration]
- 2 Cases to Execute Command Stored in Bash Variable
- How to Store Command Output to Bash Variable? [3 Examples]
- How to Read a File into Bash Variable? [2 Simple Methods]
- How to Write Bash Variable to File? [3 Effective Methods]
- Compare Variables in Bash Scripts [3 Practical Cases]
- Increment Variable Value in Bash Scripts [4+ Examples]
- Adding 1 to Bash Variable [3 Examples]
- Decrement Variable Value in Bash Scripts [4+ Examples]
- Addition of Bash Variable [4+ Examples]
- How to Subtract Two Bash Variables? [4+ Easy Approaches]
- How to Multiply Variable in Bash [6+ Practical Examples]
- Variable Substitution in Bash [Replace Character & Substring]
<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial