Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does bash have a way to un-export a variable without unsetting it?

Is it possible to export a variable in Bash, then later un-export it, without unsetting it entirely? I.e. have it still available to the current shell, but not to sub-processes.

You can always do this, but it's ugly (and I'm curious):

export FOO  #...  _FOO=$FOO unset FOO FOO=$_FOO 

Answers about other shells also accepted.

like image 384
Brendan Avatar asked Feb 10 '16 17:02

Brendan


People also ask

How do you unset a variable in shell script?

Unsetting or deleting a variable directs the shell to remove the variable from the list of variables that it tracks. Once you unset a variable, you cannot access the stored value in the variable. The above example does not print anything. You cannot use the unset command to unset variables that are marked readonly.


1 Answers

export -n FOO 

From help export:

Options:

  • -f refer to shell functions
  • -n remove the export property from each NAME
  • -p display a list of all exported variables and functions
like image 69
John Kugelman Avatar answered Sep 28 '22 09:09

John Kugelman