In Bash, we have startup scripts, that run every time user login or open a bash session. These scripts allow us to modify our bash environment and change it to fit our need, or make sure things will run every time user login/logout or open session.
Bash startup files, divided into 2, system based startup files and user based.
The system based files are running in the startup of all users. but the user based files are running only for the user (when user login, or open shell).
Also the user startup scripts are in the home directories of the users, the system startup scripts on the other hand are in the etc folder, or in its sub folder.
They are also divided into script that run for login shell, and script that run for non login shell (i.e. when you open a graphic terminal).
System startup file for login bash are:
- all scripts in /etc/profile.d/ directory.
System startup file for non login bash are:
- /etc/bash.bashrc – this file run for non login shell, for all users.
So the system startup script run for all users.
User start-up scripts are run for specific users, and must be include in the user home directory.
for non-login shell there:
- .bashrc – this script if include in the home directory of the user will run at the beginning of all non login shell of the user.
for login shell, one from the following script will run, the first one that will be found, in the following order:
It is important to remember that only one of this scripts will run, the first that will be found, when the search in the same order of the above.
During logout, bash run the following script if exist in the user home directory:
so every time the user log out, this script is run if such script exist in the user home directory. Of course it will run only for login shell.
When we run the command bash, then we are starting a new non-login shell.
If we want to run a new login shell without login, then we can run bash -l
So when run it with the “-l”, then it will run the startup script for login shell, as well the .bash_logout script when existing.