Set environment variables such as PATH in bash
The are multiple ways to define environement variables such as
PATH in the terminal. However, some ways may be more appropriate than others.
For the short answer: set the environment variables in
Now, the long answer. It is common to define variables in
~/.bashrc, however it has some disavantages. The latter file is read by
bash, and only if the shell is interactive and non-login. The login script
~/.profile is the one originally used by
/bin/sh, so it is compatible with more shells.
Files related to
bash are described in the
FILES section of
man bash. In the default
~/.profile file in Debian/Ubuntu distribution, you can read:
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
~/.bash_profile exists, you may want to include the instructions from
~/.profile as well. To do so, you can add the following to
if [ -r ~/.profile ]; then . ~/.profile; fi
To compile and install libraries in my home directory, I used to create a
~/.local folder that is analog to
/usr/local. I want to have the same structure, with
lib folders, and recognized in the same way. This is possible by adding the following content in
Some remarks on environment variables:
gccwhere to search for both C and C++ header files.
gccwhere to search for C header files.
gccwhere to search for C++ header files.
LIBRARY_PATHis used by
gccbefore compilation to find directories containing static and shared libraries to be linked.
LD_LIBRARY_PATHis used by programs to find directories containing shared libraries.
Note that this applies also to
clang. I commented the line defining
$HOME/.local/bin was already added in the default configuration of Debian/Ubuntu. It is not necessary to
export PATH since it is enough that it is exported once and it can add complications on shells other than bash. For other variables such as
CPATH, I had to use
export, so I added the following to
export CPATH export C_INCLUDE_PATH export CPLUS_INCLUDE_PATH export LIBRARY_PATH export LD_LIBRARY_PATH
Softwares such as CPLEX do not follow strictly the UNIX structure when installed. No problem, I add the corresponding paths in my
Feel free to modify the
CPLEX_ARCH variables according to your specific configuration.
.bash_profile vs .bashrc - Josh Staiger ↩︎
What's the difference between .bashrc, .bash_profile, and .environment? - Stack Exchange ↩︎
What are the functional differences between .profile .bash_profile and .bashrc - Stack Exchange ↩︎
bash - Linux man page ↩︎
Difference between .bashrc and .bash_profile - Stack Exchange ↩︎
LD_LIBRARY_PATH vs LIBRARY_PATH - Stack Exchange ↩︎
Environment Variables Affecting GCC ↩︎
clang manual ↩︎
How to correctly add a path to PATH? - Stack Exchange ↩︎