0% found this document useful (0 votes)
6 views

Bash - Using Export in .Bashrc - Unix & Linux Stack Exchange

The document discusses when to use export in a .bashrc file. Export is only needed for variables that should be available to other programs launched from the shell, not for shell internal variables. Common examples of variables that typically need export are environment variables like PATH and HOME, while shell-specific variables like PS1 do not.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Bash - Using Export in .Bashrc - Unix & Linux Stack Exchange

The document discusses when to use export in a .bashrc file. Export is only needed for variables that should be available to other programs launched from the shell, not for shell internal variables. Common examples of variables that typically need export are environment variables like PATH and HOME, while shell-specific variables like PS1 do not.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unix & Linux Stack Exchange is a question and Anybody can ask a question

answer site for users of Linux, FreeBSD and


other Un*x-like operating systems. It only takes
a minute to sign up. Anybody can answer

Sign up to join this community The best answers are voted up and
rise to the top

Using export in .bashrc


Asked 10 years, 2 months ago Modified 8 days ago Viewed 92k times

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

70 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 .

bash environment-variables 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

3 Answers Sorted by: Highest score (default)


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
78 export ed.

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.

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.

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?

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.

Share Improve this answer Follow answered Jan 4, 2014 at 16:48


Zelda
6,258 1 23 28

You might also like