55

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.

Brendan
  • 1,995
  • 1
  • 20
  • 35
  • 4
    The [`dash` manual page](http://linux.die.net/man/1/dash) explicitly states: *"the only way to un-export a variable is to unset it."* – tripleee Feb 10 '16 at 17:52
  • Regarding other shells: http://stackoverflow.com/q/33395668/1126841 – chepner Feb 10 '16 at 18:25

1 Answers1

66
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
Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578