Eons ago, when I first started assigning variables in bash, it was something along the lines of:

export EDITOR=nano
export PS1=something

...and the likes. And the habit of exporting has kinda stuck with me.

However, now that I'm older, and hopefully a tad wiser, I know that it's not required, so my question is then: When is it required? When is it not? I rarely do variable assignments outside of simple .bashrcstuff, as any scripts I write are done in perl instead.


1 Answer 1


It's hard to answer that question completel and accurately, because the point of doing export is to put a name/value pair (EDITOR=nano for example) into the information inherited by a child process of the exporting shell.

In general, you want to export things you set in .bashrc: EDITOR is a great example because that sets your preference for text editors that mailers, database interfaces and many other programs use to decide what program to run when one of those programs wants you to edit some text. I use EDITOR=vi myself. Other common examples from .bashrc are ORACLE_HOME, PATH, VISUAL, TERM and SHELL. I got those by doing man more, and reading down to the ENVIRONMENT section.

Now that I've written "in general", I have to note that other than EDITOR and VISUAL, environment values are many, varied and specific to some subsystem. Doing anything with the notorious Oracle database system requires lots of extra environment values, a lot of it by superstition. Because the shell's environment is a set of name/value pairs, the environment is used by individual systems in all kinds of different ways. Apache web server passes a lot of values to CGI-BIN programs in the environment.

My advice is to export as few variables as possible. Don't pollute your interactive environment, as you can never tell when some program will decide to use your secret environment variable's value. Write small shell scripts that do little more than set environment variables then run the program that expects those variables.

If you have a large number of environment variables set for your interactive environment, you will be surprised if you try to run something from cron - crond will not set the environment correctly, and you will have no idea of why.

  • I nowadays use vim, but way back i used nano, and I remember that crontab -e was done with nano after setting EDITOR. Am I to understand that if I hadn't used export, then crontab -e would have used vi instead (or whatever the default is... this was on FreeBSD, so maybe ee)?
    – Jarmund
    Apr 19, 2015 at 19:05
  • @Jarmund - I believe that's correct. man crontab gives me this: Edits the current crontab using the editor specified by the VISUAL or EDITOR environment variables. I still occasionally have to export something in .profile or '.zshenv` or whatever to get some random program to work the way I want it to.
    – user732
    Apr 19, 2015 at 19:28
  • alright, so for me I should always use export, since, like I said earlier, I only export variables in .bashrc. I guess the only exception would be if I'm just testing something and therefore want to set a variable temporarily.
    – Jarmund
    Apr 19, 2015 at 19:55

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