71

I have noticed in my .bashrc that some lines have export in front of them, such as

export HISTTIMEFORMAT="%b-%d  %H:%M  "
...
export MYSQL_HISTFILE="/root/.mysql_history"

whereas others don't, such as

HISTSIZE=100000

I am wondering if, first, this is correct, and second what the rule is for using export in .bashrc.

Martin Vegter
  • 358
  • 75
  • 236
  • 411

3 Answers3

79

You only need export for variables that should be "seen" by other programs which you launch in the shell, while the ones that are only used inside the shell itself don't need to be exported.

This is what the man page says:

The supplied names are marked for automatic export to the environment of subsequently executed commands. If the -f option is given, the names refer to functions. If no names are given, or if the -p option is supplied, a list of all names that are exported in this shell is printed. The -n option causes the export property to be removed from each name. If a variable name is followed by =word, the value of the variable is set to word. export returns an exit status of 0 unless an invalid option is encountered, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a function.

This can be demonstrated with the following:

$ MYVAR="value"
$ echo ${MYVAR}
value
$ echo 'echo ${MYVAR}' > echo.sh
$ chmod +x echo.sh
$ ./echo.sh

$ export MYVAR="value-exported" $ ./echo.sh value-exported

Explanation:

  • I first set ${MYVAR} to be a shell variable with MYVAR="value". Using echo I can echo the value of it because echo is part of the shell.
  • Then I create echo.sh. That's a little script that basically does the same, it just echoes ${MYVAR}, but the difference is that it will run in a different process because it's a separate script.
  • When calling echo.sh it outputs nothing, because the new process does not inherit ${MYVAR}
  • Then I export ${MYVAR} into my environment with the export keyword
  • When I now run the same echo.sh again, it echoes the content of ${MYVAR} because it gets it from the environment

So to answer your question:

It depends where a variable is going to be used, whether you have to export it or not.

ilkkachu
  • 138,973
replay
  • 8,553
  • 1
  • 27
  • 31
37

Use export for environment variables. Environment variables are an operating system feature. Environment variables are inherited by child processes: if you set them in a shell, they're available in all the programs started by this shell. Variables used by many applications or by specific applications other than shells are environment variables. Here are a few examples of common environment variables:

  • HOME — indicates the user's home directory, which is where per-user configuration files are located. Used by any program that reads per-user configuration files or otherwise needs to know the location of the user's home directory.
  • PATH — indicates where to find executable files to launch other programs. Used by every program that needs to start another program.
  • LD_LIBRARY_PATH — indicates where to find dynamic library files. Used by every dynamically linked executable.
  • EDITOR, VISUAL — indicates what program to run when an editor is needed. Used by any program that needs to launch a text editor.
  • DISPLAY, XAUTHORITY — indicates how to connect to the X11 server. Used by X11 clients (i.e. GUI programs).
  • LESS — options automatically turned on when less is run. Used by less.
  • http_proxy — indicates the web proxy to use. Used by most web browsers.

Do not use export for shell variables. Shell variables are a feature of the shell as a programming language. Shell variables are used only inside the shell where they are set; they have no meaning to programs launched by the shell. Shell variables are duplicated when a subshell is created, like the rest of the shell state. Here are a few examples of shell variables that have a meaning to popular shells:

  • PS1 — the prompt to display before each command.
  • IFS — the characters that separate words in unquoted variable expansions and command substitutions.
  • HISTFILE — a file where the shell will write the command history.

In addition to variables that are used by the shell, most shell scripts use variables for their internal purposes.

Most environment variables (e.g. PATH) make sense for a whole session, and should be set in ~/.profile or a similar file. Variables that make sense only to a specific shell (e.g. PS1) should be set in a shell-specific file such as ~/.bashrc or ~/.zshrc. See Is there a ".bashrc" equivalent file read by all shells?

8

For bash internal variables you do not need export. From your example HISTTIMEFORMAT is used by bash itself and does not need an export MYSQL_HISTFILE is for mysql and that needs export otherwise mysql does not see it.

Zelda
  • 6,262
  • 1
  • 23
  • 28