The “unset” Command in Linux [2 Practical Examples]

The unset command is a built-in command in Linux that is used to unset values and attributes of shell variables and functions. It is useful for unsetting variables or functions if they are no longer required.

In this article, I’ll demonstrate the most common applications of the unset command in Linux with some practical examples.

Syntax of “unset” Command

The syntax of the unset command in Linux is:

unset [-f] [-v] [-n] [name ...]

Note: Everything inside square brackets is optional in this syntax. Three dots after the name indicate that multiple values can be passed to the argument.

Options of “unset” Command

You can use the unset command without any options. In that case, the unset command first tries to unset a variable, and if that fails, then it tries to unset a function.

To check out all the available options, you can browse the help page for set command:

help set

In the help page, the listed unset command options are:

Options Description
-f Treats each NAME as a shell function
-v Treats each NAME as a shell variable
-n Treats each NAME as a name reference and unsets the variable itself rather than the variable it references

2 Examples of Using the “unset” Command in Linux

Let’s explore how you can use the unset command to unset the variables or functions:

1. Removing Shell Variables

To unset the shell variables, you can use the unset command with or without the option -v, the result will be the same. To unset the shell variables, first, open the Ubuntu Terminal and then set the variables. For example:

year=2024

After setting values for the variable, check it:

echo "$year"

Now to unset the variable, use the unset command:

unset year

Finally, check the variable again:

echo "$year"

using the unset command to unset a variable

In the image above, you can see, an empty line is returned as the variable “year” was unset earlier.

2. Removing Functions

To unset the shell functions, you can use the unset command with or without the option -f. The distinction is that if you use the unset command without any options, it will first try to unset a variable, and if that fails, it will attempt to unset a function. However, if you use the unset command with the option -f, it will attempt to unset a function in the first place. Let’s open the nano window and write a function first:

nano greetings.sh

Inside the nano window, you can define the functions in a script like this:

#!/bin/bash

#Defining the function
welcome_note() {
	echo "Welcome!"
}

#Calling the function
echo "The function is called for the first time:"
welcome_note

#Unsetting the function
unset -f welcome_note
echo "The function is unset"

#Calling the function after unsetting it
echo "The function is called after unsetting it:"
welcome_note
EXPLANATION

In this script named “greetings.sh”, I’ve defined the welcome_note function and called it once. Then, after unsetting the function, I attempted to call the welcome_note function again.

Now execute the script “greetings”:

./greetings.sh

using the unset command to unset a function

From the image, you can see, the welcome_note function works properly the first time, returning the string “Welcome!”. However, an error message appeared when attempting to call the welcome_note function again after unsetting it.

Conclusion

In this article, I’ve demonstrated the unset command in Linux through some practical examples. The unset command, as the name suggests, is useful to unset the variables and functions. Try out the unset command and its options yourself. Hopefully, this article will help you in utilizing the unset command.

People Also Ask

What are set and unset in Linux?

The set and unset commands are two built-in commands in Linux. You can use the set command to set or unset the values of shell options and positional parameters. And, the unset command unsets the shell variables and functions.

How do you unset a function in Linux?

To unset a function in Linux, you can use the unset command with the option -f followed by the function’s name. This will remove the function from the current shell environment.

How do I unset a variable in Bash?

To unset a variable in Linux, you can use the unset command followed by the variable’s name. This will remove the variable from the current shell environment.

How to unset a variable using the set command?

To unset a variable using the set command, you can use the set command with the option -n.

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Avatar photo

Hadi Ul Bashar holds a BSc degree in Biomedical Engineering from Bangladesh University of Engineering and Technology. In January 2023, Bashar joined Softeko as an Excel and VBA content developer. Since then, he has authored over 50 articles and created over 70 videos. Also, he has an interest in Linux. Currently, he is working as a Linux content developer executive. Here, he aims to create useful Linux content for its users. Reading books and travelling are his favourite pastimes. Read Full Bio

Leave a Comment