15

I want to know how to clear all variables which I defined in command prompt without closing terminal ?

for example, if I set a variable in command prompt as:

$ a=1

now I want to delete the variable $a (and many other variables defined in similar way) without closing terminal. I could use unset but it will be hectic if there are large no. of variables

13
  • 2
    exec "$0"maybe - but it's hard to tell what you mean by global.
    – mikeserv
    Commented Dec 10, 2014 at 23:07
  • 3
    Why do you want to do that? Commented Dec 10, 2014 at 23:12
  • 1
    @HaukeLaging I am working with arrays using script. I sometimes add one element to it. because of which I cant run the script with old values(since array is changed). i have to close the terminal and start a new session again. I hope I made clear. if not please let me know :)
    – Alex Jones
    Commented Dec 10, 2014 at 23:18
  • 1
    @muru - he gets a list saved to a $var with var=$(declare -p +F); then passes that as an argument to a function which does echo "${2%%=*}". That answer has a few problems with sheer size - but it should work if your environment isn't huge.
    – mikeserv
    Commented Dec 10, 2014 at 23:34
  • 3
    I have no idea what you're trying to do. Are you talking about an interactive shell or a script? Variables set in a script executed from an interactive shell don't affect the parent shell. Tell us what you want to achieve, not which dead end you're pursuing. Commented Dec 10, 2014 at 23:49

2 Answers 2

20

If you do (GNU coreutils)

exec env --ignore-environment /bin/bash

you will use a fresh & new environment

3
  • what about exec "$0" ?
    – Alex Jones
    Commented Dec 11, 2014 at 21:04
  • 1
    @edwardtorvalds $0 is the zeroth argument to an executed shell. So, if you're running bash $0 is bash (probably - it might be -bash in a -login shell case depending on version). If you're running a script with a #!/bin/bash bangline, $0 is the path to the script. exec "$0" is probably more flexible, but exec bash is more explicit. If you enter that command at a terminal, bash should recognize it's in an interactive environment and do the equivalent of bash -i - which will get your ~/.bashrc file run as well.
    – mikeserv
    Commented Dec 11, 2014 at 21:32
  • This doesn't help with export a=123, that variable is inherited...
    – stimur
    Commented Feb 27, 2020 at 21:32
1

You could use env, which is provided by GNU coreutils (typically preinstalled on GNU/Linux systems):

exec env --ignore-environment /bin/bash

The exec system call is so that your current process is replaced in-place by the new, environment-less version of your shell.

This has the benefit to also clear any exported environment variables.

3
  • But this would clear my modified PATH variable, for example export PATH=$PATH:~/foo, right?
    – Alex Jones
    Commented Feb 27, 2019 at 18:23
  • 1
    You are right. I've modified my answer so that the absolute path of the shell is used instead of $0.
    – user30747
    Commented Feb 27, 2019 at 18:50
  • 2
    Thanks to user30747, stolen your answer to correct mine. Can't delete accepted answer Commented Feb 29, 2020 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .