Bash - Using Export in .Bashrc - Unix & Linux Stack Exchange
Bash - Using Export in .Bashrc - Unix & Linux Stack Exchange
Sign up to join this community The best answers are voted up and
rise to the top
I have noticed in my .bashrc that some lines have export in front of them, such as
HISTSIZE=100000
I am wondering if, first, this is correct, and second what the rule is for using export in
.bashrc .
Share Improve this question edited Aug 24, 2019 at 20:33 asked Jan 4, 2014 at 16:44
Follow Matthias Braun Martin Vegter
8,229 7 48 58 378 75 235 409
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.
$ 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}
It depends where a variable is going to be used, whether you have to export it or not.
Share Improve this answer Follow edited Mar 3 at 15:59 answered Jan 4, 2014 at 16:50
ilkkachu replay
138k 15 241 407 8,533 1 27 31
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,
36 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.
DISPLAY , XAUTHORITY — indicates how to connect to the X11 server. Used by X11 clients
(i.e. GUI programs).
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:
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?
Share Improve this answer Follow edited Apr 13, 2017 at 12:37 answered Apr 19, 2015 at 23:36
Community Bot Gilles 'SO- stop being
1 evil'
827k 195 1.7k
2.2k
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
8 export otherwise mysql does not see it.