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 ~/.profile
.
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[1]. The login script ~/.profile
is the one originally used by /bin/sh
[2][3], so it is compatible with more shells.
Files related to bash
are described in the FILES
section of man bash
[4]. 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
# exists.
Thus, if ~/.bash_profile
exists, you may want to include the instructions from ~/.profile
as well. To do so, you can add the following to .bash_profile
[5]:
if [; then ; 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 bin
, include
, lib
folders, and recognized in the same way. This is possible by adding the following content in ~/.profile
:
# ~/.local
# PATH="$HOME/.local/bin:$PATH"
CPATH=" /.local/include:"
C_INCLUDE_PATH=" /.local/include:"
CPLUS_INCLUDE_PATH=" /.local/include:"
LIBRARY_PATH=" /.local/lib:"
LD_LIBRARY_PATH=" /.local/lib:"
Some remarks on environment variables[6][7]:
CPATH
tellsgcc
where to search for both C and C++ header files.C_INCLUDE_PATH
tellsgcc
where to search for C header files.CPLUS_INCLUDE_PATH
tellsgcc
where to search for C++ header files.LIBRARY_PATH
is used bygcc
before compilation to find directories containing static and shared libraries to be linked.LD_LIBRARY_PATH
is used by programs to find directories containing shared libraries.
Note that this applies also to clang
[8]. I commented the line defining PATH
because $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[9]. For other variables such as CPATH
, I had to use export
, so I added the following to ~/.profile
.
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 ~/.profile
:
# CPLEX
CPLEX_PATH="/opt/ibm/ILOG/CPLEX_Studio129"
CPLEX_ARCH="x86-64_linux"
PATH=" : /cplex/bin/ : /opl/bin/"
CPATH=" : /cplex/include: /concert/include"
C_INCLUDE_PATH=" : /cplex/include: /concert/include"
CPLUS_INCLUDE_PATH=" : /cplex/include: /concert/include"
LIBRARY_PATH=" : /cplex/lib/ /static_pic: /concert/lib/ /static_pic"
LD_LIBRARY_PATH=" : /cplex/lib/ /static_pic: /concert/lib/ /static_pic"
Feel free to modify the CPLEX_PATH
and CPLEX_ARCH
variables according to your specific configuration.