2 Ways to Unset Environment Variables Using Bash Script

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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:

  1. At first, launch an Ubuntu Terminal.
  2. Write the following command to open a file in Nano:
    nano unset_variable.sh
  3. 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.

  4. Press CTRL+O and ENTER to save the file; CTRL+X to exit.
  5. Use the following command to make the file executable:
    chmod u+x unset_variable.sh
  6. Now, 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

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

Unset the Environment FunctionUpon 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
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.

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.

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


<< Go Back to Using Variables in Bash Scripting | Bash Variables | Bash Scripting Tutorial

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