When setting up a variable in .bashrc
, should I use this?
export VAR=value
Or would this be enough?
VAR=value
What is exactly the difference (if there is one)?
bashenvironment-variableslinuxshell
When setting up a variable in .bashrc
, should I use this?
export VAR=value
Or would this be enough?
VAR=value
What is exactly the difference (if there is one)?
Best Answer
The best way
The difference
Doing
only sets the variable for the duration of the script (
.bashrc
in this case). Child processes (if any) of the script won't have VAR defined, and once the script exitsVAR
is gone.explicitly adds
VAR
to the list of variables that are passed to child processes. Want to try it? Open a shell, doThe new shell gets the default prompt. If instead you do something like
the new shell gets the prompt you just set.
Update: as Ian Kelling notes below variables set in
.bashrc
persist in the shell that sourced.bashrc
. More generally whenever the shell sources a script (using thesource scriptname
command) variables set in the script persist for the life of the shell.