Fundamentals 2 min read

Mastering the unset Command: How to Delete Variables and Functions in Linux

The article explains the purpose, syntax, options, and practical examples of the Linux unset command, showing how to remove shell variables, environment variables, and functions, while noting its limitations with read‑only variables and providing step‑by‑step demonstrations.

Open Source Linux
Open Source Linux
Open Source Linux
Mastering the unset Command: How to Delete Variables and Functions in Linux

unset: Delete Variables or Functions

Function Description

The unset command is used to delete defined shell variables (including environment variables) and shell functions. It cannot delete shell variables or environment variables that have the read‑only attribute.

Command Syntax

unset [options] [name ...]

Option Meanings

-f : delete only functions

-v : delete only variables (excluding read‑only variables)

-n : delete variable names that have the reference attribute (if this option is present)

Parameter Meaning

name (optional): the variable or function to delete.

Reference Example

// (1) Set environment variable
# ag="df -h"

// (2) Use environment variable
$ ag
# (output of df -h omitted for brevity)

// (3) View current environment variables
set
# (output omitted for brevity)

// (4) Delete environment variable
unset ag

// (5) Delete function
function show_result(){ echo 'Last Command Return: $?'; }
unset -f show_result
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

linuxShellfunctionsVariablesunsetCommand
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.