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.
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_resultSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
