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

Braiam
  • 35,991
Alex Jones
  • 6,353
  • 2
    exec "$0"maybe - but it's hard to tell what you mean by global. – mikeserv Dec 10 '14 at 23:07
  • 3
    Why do you want to do that? – Hauke Laging Dec 10 '14 at 23:12
  • theyre not global - they're defined somewhere. probably in your profile or rcfile. if so then exec is what you want. I once did an answer in detail on this. maybe look - it was all about clearing the shells memory. – mikeserv Dec 10 '14 at 23:13
  • 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 Dec 10 '14 at 23:18
  • Related: http://unix.stackexchange.com/questions/172244/how-can-i-print-only-variables-defined-inside-my-shell-script/172438#172438 – muru Dec 10 '14 at 23:25
  • I would, if I understood what Gilles did. But whatever he did, if you can get a list of variables defined by you using it, then probably you can use unset on them. – muru Dec 10 '14 at 23:28
  • @muru didn't understood thats why I asked :( – Alex Jones Dec 10 '14 at 23:30
  • 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 Dec 10 '14 at 23:34
  • @mikeserv your very first comment exec "$0" works. can you please explain it in answer how ? – Alex Jones Dec 10 '14 at 23:36
  • 1
    @edwardtorvalds - not like that - that was just a naive command - you need a little more to it to do it right.=. I did explain it - in the link I posted above. It's explained pretty well (I hope). I'd rather not do it twice. Will you read it please and let me know if you need any more help or whatever? Particularly you'll want not to do the set part in the link I left - that's what migrates vars from old shell to new. – mikeserv Dec 10 '14 at 23:37
  • 1
    The short explanation is also here: http://stackoverflow.com/questions/15222328/bash-reset-reloading-bash-completely-alias-and-function#comment21462412_15225461 – jimmij Dec 10 '14 at 23:42
  • 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. – Gilles 'SO- stop being evil' Dec 10 '14 at 23:49
  • 1
    @Gilles my apologies I should not have mentioned "script". – Alex Jones Dec 10 '14 at 23:57

2 Answers2

20

If you do (GNU coreutils)

exec env --ignore-environment /bin/bash

you will use a fresh & new environment

  • what about exec "$0" ? – Alex Jones Dec 11 '14 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 Dec 11 '14 at 21:32
  • This doesn't help with export a=123, that variable is inherited... – stimur Feb 27 '20 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.

user30747
  • 260