Bash Startup Files
On many Linux systems, Bourne-related shell environments start getting populated in /etc/profile
. This file is sourced by the shell environment.
Sourcing means a script uses another file as though that file was written directly inside the script.
Commonly /etc/profile
, which is in shell script format, sources additional files from /etc/profile.d
. These are system-wide files that establish the user environment.
Other system-wide files might include /etc/bashrc
or /etc/environment
. Commonly /etc/bashrc
is sourced through /etc/profile.d
or user bash startup files. The /etc/environment
file is used only with PAM and is not explicitly part of the Bourne-related shell environment.
On the user side the bash startup files include the following:
$HOME/.bash_login
$HOME/.bash_logout
$HOME/.bash_profile
$HOME/.bashrc
$HOME/.profile
The $HOME
environment variable is defined in /etc/passwd
. When creating new user accounts there might be a file /etc/default/useradd
that defines the root directory of user home directories. Commonly this variable location is /home
and that location is the default when not explicitly defined. The user’s home directory is always a subdirectory in this parent location.
Bash supports retaining a history of user commands. The default environment variable is $HOME/.bash_history
but can be defined to a different location with the HISTFILE
environment variable.
There are no hard-set rules, but there are generally accepted guidelines for the bash startup files:
/etc/profile: used for system-wide initialization and containing global/system environment variables and startup programs.
/etc/bashrc: generally used for system-wide aliases and functions. The file might be used to modify /etc/profile
, which is provided through the upstream distro and might be automatically updated through package updates.
$HOME/.bash_profile: generally used to provide specific support related to logging in, and for local/personal environment variables and startup programs called only during the login process.
$HOME/.bash_login: can be used in addition to .bash_profile
if the file exists, but generally used as a substitute for .bash_profile
.
$HOME/.profile: can be used in addition to .bash_profile
if the file exists, but generally used as a substitute for .bash_profile
.
$HOME/.bashrc: generally used for anything local/personal and peculiar to the user’s shell environment. This file usually sources /etc/bashrc
.
$HOME/.bash_logout: generally used to provide specific support related to logging out.
These files are used for shell operations and unless explicitly sourced will not be directly used with graphical user logins and environments. The files will be sourced when a terminal window is launched.
As explained in the bash man page, the login command programmatically sources /etc/profile
and then only $HOME/.bash_profile
, $HOME/.bash_login
, or $HOME/.profile
, and in that specific search order.
A basic approach to using these files:
- Bash always sources
/etc/profile
during the login and all environment variables exported in that process remain active throughout the login session. - Use
$HOME/.bash_profile
to source$HOME/.bashrc
. - Use
$HOME/.bashrc
to source/etc/bashrc
. - Use
$HOME/.bash_logout
to execute specific logout tasks.
The bash startup scripts do not need to be executable (chmod +x
) because they are sourced. Be sure to chmod +x
any shell script called from within these scripts that need executable permissions.
Posted: Tutorial Tagged: General
Category:Next: Frozen Desktops
Previous: No Network Interface