How to undefine and unset a bash environment variable on Linux or Unix

I have set the shell variable. I no longer need the variable. How do I undefine or unset a variable in bash on a Linux or Unix-like desktop system?

 

Creating and setting variables within a script (or shell prompt) is fairly simple. The syntax is:

varName=someValue

export varName

export vech= Bus

echo  $vech

How do I unset  variables in bash?

Use unset command to delete the variables during program execution. It can remove both functions and shell variables. The syntax is:

unset varName

To unset the $vech, enter:

unset vech

Verify it:

echo $vech

A note about readonly variables

Please note that some variables cannot be unset if they are defined as readonly by the system or sysadmin:

unset BACKUPDIR

Sample outputs:

-bash: unset: BACKUPDIR: cannot unset: readonly variable

You can define your readonly variables using the following syntax:

declare -r BACKUPDIR= foo

How do I unset a shell function on bash?

The syntax is as follows to treat each varName as a shell function:

unset -f varName

unset -f tarhelper

For more info, type:

 

 

 

 

To add a variable which will be available for remote login sessions (i.e. when you ssh to the user from remote system), modify .bash_profile file.

$ vi .bash_profile

Add the following line to .bash_profile file at the bottom.

export VAR2='This is TecMint Home'

When on sourcing this file, the variable will be available when you ssh to this user, but not on opening any new local terminal.

$ source .bash_profile 
$ echo $VAR2

Here, VAR2 is not initially available but, on doing ssh to user on localhost, the variable becomes available.

$ ssh tecmint@localhost
$ echo $VAR2

Export User Wide Variables in Bash Profile

To remove this variable, remove the line from /etc/profile file and re-source it.

c.) However, if you want to add any environment which you want to be available all throughout the system, on both remote login sessions as well as local sessions( i.e. opening a new terminal window) for all users, just export the variable in /etc/environment file.

export VAR12='I am available everywhere'

Add System Variable in Environment File

After that just source the file and the changes would take effect.

$ source /etc/environment
$ echo $VAR12
$ sudo su
$ echo $VAR12
$ exit
$ ssh localhost
$ echo $VAR12

Check Environment Variable for All Users

Here, as we see the environment variable is available for normal user, root user, as well as on remote login session (here, to localhost).

To clear out this variable, just remove the entry in the /etc/environment file and re-source it or login again.

NOTE: Changes take effect when you source the file. But, if not then you might need to log out and log in again.

Conclusion

Thus, these are few ways we can modify the environment variables. If you find any new and interesting tricks for the same do mention in your comments.

Leave a Reply

Your email address will not be published. Required fields are marked *