2 Ways to Unset Environment Variables Using Bash Script

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!

Key Takeaways

  • Detailed discussion on unsetting environment variables as well as functions using a bash script.
  • Getting comprehensive guidance to unset environment variables using unset and set -n Command.

Free Downloads

2 Methods to Unset Environment Variable Using Bash Scripts

In Bash scripting, understanding how to unset environment variables is essential for effective configuration management. This section delves into the intricacies of unsetting variables, focusing on two distinct methods: the direct application of the unset command and the set command with the -n flag.

You can read the Comparative Analysis of Methods section to distinguish between these methods and best pick one for your need. 

Method 01: Using the unset Command to Remove Environment Variables

In Bash scripting, 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

In our first case, I will unravel the process of eliminating shell variables, offering insights into how to effectively clear the way for optimized scripting and configuration control.

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a file in Nano:

nano unset_variable.sh
EXPLANATION
  • nano: Opens a file in the Nano text editor.
  • unset_variable.sh: Name of the file.

❸ 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
EXPLANATION

The 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 the unset 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
EXPLANATION
  • chmod: changes the permissions of files and directories.
  • u+x: Here, u refers to the “user” or the owner of the file and +x specifies the permission being added, in this case, the “execute” permission. When u+x is added to the file permissions, it grants the user (owner) permission to execute (run) the file.
  • unset_variable.sh: is the file name.

❻ Run the script by the following command:

./unset_variable.sh

Unsetting of the Shell VariablesThe 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

Following the previous case, this section guides you through the process of unsetting the environment function using a bash script.

You can follow the steps of case 01 of method 01, to save & make the script executable.

Script (unset_function.sh) >

#!/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
EXPLANATION

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.

Run the script with the following command.

./unset_function.sh

Unset the Environment FunctionUpon calling the function for the first time, it returns 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: Use the set Command with -n Flag to Remove Environment Variables

Alternatively, you can unset environment variables by utilizing the set command along with the -n option. Follow the below script to get a clear overview.

You can follow the steps of case 01 of method 01, to save & make the script executable.

Script (set-n_flag.sh) >

#!/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
EXPLANATION

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.

Run the file by using the following command.

./set-n_flag.sh

Use the set Command with -n Flag to remove the environment variable.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.

Comparative Analysis of the Methods

Lastly, here’s a comparative table outlining the pros and cons of using the unset command and the set command with the -n flag for unsetting environment variables in Bash:

Methods Pros Cons
Method 1
  • Direct and immediate removal.
  • Straightforward for small scripts.
  • Risk of accidentally removing important variables.
  • Irreversible once a variable is unset.
Method 2
  • Suitable for complex scripts.
  • Prevents unintended variable removal.
  • Doesn’t actually unset variables; may be perceived as indirect.
  • Doesn’t offer the same directness as unset.

Deciding between Method 1 and Method 2 depends on how careful you want to be and how complicated your script is. If you want to quickly and directly remove a variable, go with Method 1. But if you’re worried about accidentally changing things, especially in complex scripts, using Method 2 is a smart way to avoid unexpected issues and ensure everything runs smoothly.

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. 
Rate this post
Mohammad Shah Miran

Hey, I'm Mohammad Shah Miran, previously worked as a VBA and Excel Content Developer at SOFTEKO, and for now working as a Linux Content Developer Executive in LinuxSimply Project. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). As a part of my job, i communicate with Linux operating system, without letting the GUI to intervene and try to pass it to our audience.

Leave a Comment