Environment Variable
07 Jun 2015 • Leave CommentsIn Unix-like systems, we have to deal with environment variables such as PATH, MANPATH etc. Here are some tips for reference.
-
Order of directory
PATH=$PATH:~/opt/bin PATH=~/opt/bin:$PATH
The diffeence of the two lines is the order when searching for commands. The 2nd line make directory
~/opt/bin
searched before those ones in originalPATH
. -
export
or notPATH=$PATH:~/opt/bin export PATH=$PATH:~/opt/bin
export
is a shell builtin forbash
,ksh
andzsh
. The first line omit theexport
keyword which is calledassignment
. We don't needexport
if the variable is already in the environment: any change of the value of the variable is reflected in the environment for modern shells.PATH
is pretty much always in the environment; all Unix-like system sets it in the very beginning (usually in the very first process, in fact).But if the variable is not in the environment, you must add
export
. For example, some environment variable is for specific program newly installed. You could test by command:```bash ~ $ declare -p VAR_NAME
Use
export
if not sure. -
$HOME
or~
export PATH="$PATH:$HOME/bin" PATH="$PATH:$HOME/bin" PATH=$PATH:$HOME/bin PATH=$PATH:~/bin PATH="$PATH:~/bin"
The last line does not work*. The
~
character is only expanded to your home directory when it's the first character of a word and it's unquoted. For the last line, the~
is between double quotes and therefore not expanded. Even if you wroteexport "PATH=$PATH:"~/Unix/homebrew/bin
, the~
would not be expanded because it is not at the beginning of a shell word.Use
$HOME
instead of~
. -
Double quotes or not
If you have
~
in variable settings, omit double quotes. In all other cases, this does not matter. ~/.profile
is the place to put stuff that applies to your whole session of a login shell (like zsh), such as programs that you want to start when you log in (but not graphical programs, they go into a different file, i.e.~/.xinitrc
), and environment variable definitions. For Bash shell, we have a counterpart.bash_profile
.~/.bashrc
is the place to put stuff that applies only to Bash only, such as alias and function definitions, shell options, and prompt settings. (You could also put key bindings there, but for bash they normally go into~/.inputrc
.)-
~/.bash_profile
can be used instead of~/.profile
, but it is Bash's alternative of~/.profile
, not used by any other shell. (This is mostly a concern if you want your initialization files to work on multiple machines and your login shell isn't bash on all of them.) This is a logical place to include~/.bashrc
if the shell is interactive.Set up your environment (PATH, LANG, EDITOR, …) in "~/.bash_profile". Set up your bash shell (PS1, functions, shopts, …) in "~/.bashrc". Then source ".bashrc" from ".bash_profile".
Check dotfiles.
-
You may see here and there recommendations to either put environment variable definitions in
~/.bashrc
or always launch login shells in terminals. Both are bad ideas. The most common problem with either of these ideas is that your environment variables will only be set in programs launched via the terminal, not in programs started directly with an icon or desktop menu or keyboard shortcut.But in Gentoo, the official Wiki recommends per-user setting in
~/.bashrc
which is sourced by~/.bash_profile
. ~/.bash_profile
is executed for login shells, while~/.bashrc
is executed for interactive non-login shells. When you login (eg: type username and password) via console, either physically sitting at the machine when booting, or remotely via ssh:.bash_profile
is executed to configure things before the initial command prompt. But, if you've already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then .bashrc is executed before the window command prompt.~/.bashrc
is also run when you start a new bash instance by typing/bin/bash
in a terminal.-
Execution order:
Login shell: /etc/profile -> /etc/env.d/* -> /etc/bash/bashrc -> /etc/profile.d/*.sh -> ~/.bash_profile -> ~/.bashrc
Interactive shell: /etc/bash/bashrc -> ~/.bashrc