Unset

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

← Export VariablesHomeGetting User Input Via Keyboard →

Use the unset command to delete the variables during program execution. It can remove both functions and shell variables. Using the unset command, you can unset values and attributes of shell variables and functions. Let us see how to use the unset command on Linux and Unix-like systems.

Purpose

Use the unset commmand to undefine a variable including function in BASH.

Syntax

The syntax is:

unset var_name_here
unset [options] var

Examples

In this example, define a bash variable named vech with 'Bus' as value:

vech=Bus

Display value using the echo command or printf command:

echo "$vech"

To unset a variable at the command line, run:

unset vech
echo $vech

Undefine shell function

The syntax is:

unset -f function_name

Test it as follows. Create a new bash function named hello():

function hello(){ echo "Hello $USER. Today is $(date)."; }

Run it:

hello

Unset or undefine bash function named hello(), run:

unset -f hello
hello
Linux Bash unset command syntax and examples
Linux Bash unset command syntax and examples

See also

← Export VariablesHomeGetting User Input Via Keyboard →