Linux unset Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the unset command in Linux to remove or delete variables and shell functions from the current shell environment. The lab covers understanding the purpose of the unset command, unsetting environment variables, and unsetting shell functions, with practical examples. This lab is part of the System Configuration and Settings skill set.

The unset command is a useful tool for managing the shell environment and cleaning up variables or functions that are no longer needed. The lab provides step-by-step instructions on how to create and unset environment variables and shell functions, demonstrating the versatility of the unset command.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/declare("`Variable Declaring`") linux/UserandGroupManagementGroup -.-> linux/env("`Environment Managing`") linux/UserandGroupManagementGroup -.-> linux/export("`Variable Exporting`") linux/UserandGroupManagementGroup -.-> linux/unset("`Variable Unsetting`") subgraph Lab Skills linux/echo -.-> lab-422980{{"`Linux unset Command with Practical Examples`"}} linux/declare -.-> lab-422980{{"`Linux unset Command with Practical Examples`"}} linux/env -.-> lab-422980{{"`Linux unset Command with Practical Examples`"}} linux/export -.-> lab-422980{{"`Linux unset Command with Practical Examples`"}} linux/unset -.-> lab-422980{{"`Linux unset Command with Practical Examples`"}} end

Understand the Purpose of the unset Command

In this step, you will learn about the purpose of the unset command in Linux. The unset command is used to remove or delete variables or shell functions from the current shell environment.

First, let's create an environment variable and then unset it:

## Create an environment variable
export MY_VAR="Hello, World!"
echo $MY_VAR

## Unset the environment variable
unset MY_VAR
echo $MY_VAR

Example output:

Hello, World!

As you can see, after running the unset MY_VAR command, the MY_VAR environment variable is no longer accessible.

Next, let's create a shell function and then unset it:

## Create a shell function
my_function() {
  echo "This is a shell function."
}
my_function

## Unset the shell function
unset -f my_function
my_function

Example output:

This is a shell function.
bash: my_function: command not found

In this example, we first define a shell function called my_function, and then we use the unset -f command to remove the function from the current shell environment.

The unset command is a useful tool for managing the shell environment and cleaning up variables or functions that are no longer needed.

Unset Environment Variables

In this step, you will learn how to unset environment variables in Linux.

Environment variables are used to store configuration information that can be accessed by the shell and other programs. Sometimes, you may need to remove or unset these variables.

Let's start by creating a few environment variables:

## Create environment variables
export VAR1="Value 1"
export VAR2="Value 2"
export VAR3="Value 3"

## List all environment variables
env

Example output:

VAR1=Value 1
VAR2=Value 2
VAR3=Value 3
...

Now, let's unset these variables one by one:

## Unset a single variable
unset VAR1
echo $VAR1

## Unset multiple variables
unset VAR2 VAR3
echo $VAR2
echo $VAR3

Example output:

As you can see, after running the unset commands, the environment variables are no longer accessible.

You can also use the unset command to remove variables from the shell's environment. This can be useful when you want to clean up your environment or remove variables that are no longer needed.

Unset Shell Functions

In this step, you will learn how to unset shell functions in Linux.

Shell functions are user-defined commands that can be called like built-in commands. They are stored in the shell's memory and can be accessed throughout the current shell session.

Let's start by creating a simple shell function:

## Define a shell function
my_function() {
  echo "This is a shell function."
}

## Call the shell function
my_function

Example output:

This is a shell function.

Now, let's unset the shell function:

## Unset the shell function
unset -f my_function
my_function

Example output:

bash: my_function: command not found

As you can see, after running the unset -f my_function command, the my_function shell function is no longer accessible.

You can also use the declare -f command to list all the shell functions currently defined in your shell environment:

## List all shell functions
declare -f

This will display all the shell functions that are currently defined in your shell.

The unset command is a useful tool for managing the shell environment and cleaning up variables or functions that are no longer needed.

Summary

In this lab, you learned about the purpose of the unset command in Linux, which is used to remove or delete variables or shell functions from the current shell environment. You first created an environment variable and a shell function, and then used the unset command to remove them. You also learned how to unset multiple environment variables at once, and how to check the result of the unset command by printing the variables. The unset command is a useful tool for managing the shell environment and cleaning up variables or functions that are no longer needed.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like