Bash remove environment variables command

See all Bash/Shell scripting related FAQ
Do you want to delete an exported bash environment variables in Linux, macOS, or Unix-like systems? Try using the “unset <VARIABLE>” command to remove the environment variable when using bash. Let us see syntax and examples in detail.

The unset command works with sh, csh/tcsh, bash and many other shells to delete environment variables.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux or Unixterminal
Category Linux shell scripting
OS compatibility Linux macOS Unix
Est. reading time 2 minutes

Bash remove environment variables command

  1. Open the terminal application.
  2. Use the set command or env command to list the currently defined environment variable. For example:
    $ set
    $ env
    $ set | grep -w "VARIABLE_NAME"
    $ env | grep -w "VARIABLE_NAME"
  3. You can also use the echo command or printf command to print variable value:
    $ echo "$VARIABLE_NAME"
    $ printf "%s\n" "$VARIABLE_NAME"
  4. To delete or remove values and attributes of shell variables, use the unset command:
    $ unset VAR_NAME
    $ unset FOO
  5. Verify that the shell variable is deleted and gone from the terminal bash session in Linux and Unix. For example:
    $ set | grep -w 'VAR_NAME'
    $ env | grep -w 'VAR_NAME'
    $ echo "$VAR_NAME"
  6. Try defining two shell variables using the declare command:
    $ _foo="This is a test"
    $ declare -i _num=10

    Print values:
    $ echo "$_foo"
    $ printf "%d\n" "$_num"
    $ set | grep '_num'
    $ set | grep '_foo'

    Next, delete those bash environment variable as follows using the unset command:
    $ unset _num
    $ unset _foo

    Verify it:
    $ echo "$_foo"
    $ set | grep '_num'

    Bash remove environment variable using unset command

Deleting exported bash environment variables

You can export bash variables to the child process or subshell using the export command. For example:
$ export _vech='I love BMW car'
$ printf "%s\n" "$_vech"
$ env | grep '_vech'
# now remove it #
$ unset _vech
$ env | grep '_vech'
$ printf "%s\n" "$_vech"

A note about read-only bash variables

Please note that some variables cannot be unset if declared read-only using the readonly command. For instance:
$ readonly _bar='VIP VALUE HERE'
$ set | grep '_bar'
$ unset _bar

You will see an error message as follows:

bash: unset: _bar: cannot unset: readonly variable

If you really need to unset such read-only bash variable without root access, try the exec command:
$ exec $BASH
$ unset _bar
$ echo "$_bar"
$ set | grep _bar
$ env | grep _bar

In ZSH you can simply do:
$ typeset +r _VAR
$ unset _VAR

Summing up

Using the unset bash command, you learned how to remove environment variables in Linux, macOS, *BSD, and Unix-like terminals. For more information about the unset command, please see the following manual pages using the man command/info command or help command $ man bash
$ info bash
$ help unset

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

0 comments… add one

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.