1. Overview

Bash shell uses a few startup files to set up the environment. These files determine certain Bash shell configurations for the shell itself and system users.

In this tutorial, we’ll learn about a few startup files such as .bashrc, .bash-profile, and .profile and their differences.

2. Interactive Shells and Non-Interactive Shells

Bash provides the option of two modes in an interactive shell, i.e., login and non-login.

When we log in to a system using ssh, we get an interactive login shell. This shell reads startup files when invoked.

However, when we invoke a new shell on an already logged-in shell, we get an interactive, non-login shell. This type of shell executes only the .bashrc file.

When a shell doesn’t need any human intervention to execute commands, we call it a non-interactive shell.  For example, when a script forks a child shell for the execution of commands, the child shell is a non-interactive shell. This shell doesn’t execute any startup file. It inherits environment variables from the shell that created it.

3. Bash Startup Files

Startup files contain commands that are to be executed on shell startup. As a result, the shell executes commands present in these files automatically to set up the shell. This happens even before displaying the command prompt.

3.1. Significance of .bash_profile

The .bash_profile file contains commands for setting environment variables. Consequently, future shells inherit these variables.

In an interactive login shell, Bash first looks for the /etc/profile file. If found, Bash reads and executes it in the current shell. As a result, /etc/profile sets up the environment configuration for all users.

Similarly, Bash then checks if .bash_profile exists in the home directory. If it does, then Bash executes .bash_profile in the current shell. Bash then stops looking for other files such as .bash_login and .profile.

If Bash doesn’t find .bash_profile, then it looks for .bash_login and .profile, in that order, and executes the first readable file only.

Let’s look into a sample .bash_profile file. Here we’re setting & exporting the PATH variable:

echo "Bash_profile execution starts.."  
export PATH; 
echo "Bash_profile execution stops.."

We’ll see the below output right before the command prompt on the interactive login shell:

Bash_profile execution starts.. 
Bash_profile execution stops.. 
[dsuser@cygnus ~]$

3.2. Significance of .bashrc

.bashrc contains commands that are specific to the Bash shells. Every interactive non-login shell reads .bashrc first. Normally .bashrc is the best place to add aliases and Bash related functions.

The Bash shell looks for the .bashrc file in the home directory and executes it in the current shell using source.

Let’s take a look inside a sample .bashrc file:

echo "Bashrc execution starts.." 
alias elui='top -c -u $USER' 
alias ll='ls -lrt' 
echo "Bashrc execution stops.."

We’ll see the below output right before the command prompt on the interactive non-login shell:

[dsuser@server ~]$ bash
Bashrc execution starts.. 
Bashrc execution stops.. 
[dsuser@server ~]$

3.3. Significance of .profile

During an interactive shell login, if .bash_profile is not present in the home directory, Bash looks for .bash_login. If found, Bash executes it. If .bash_login is not present in the home directory, Bash looks for .profile and executes it.

.profile can hold the same configurations as .bash_profile or .bash_login. It controls prompt appearance, keyboard sound, shells to open, and individual profile settings that override the variables set in the /etc/profile file.

4. Differences

On every interactive login, the Bash shell executes .bash_profile. If .bash_profile is not found in the home directory, Bash executes the first readable file found from .bash_login and .profile. Whereas, on every interactive non-login shell startup, Bash executes .bashrc.

Generally, environment variables are put into .bash_profile. Since the interactive login shell is the first shell, all the default settings required for the environment setup are put in .bash_profile. Consequently,  they are set only once but inherited in all child shells.

Likewise, aliases and functions are put into .bashrc to ensure that these are loaded every time a shell is launched from within the existing environment.

However, to avoid login and non-login interactive shell setup difference, .bash_profile calls .bashrc. As a result, we’ll see the below code piece is inserted in .bash_profile, so that on every interactive login shell, .bashrc is also executed in the same shell:

if [ -f ~/.bashrc ];
    .  ~/.bashrc; 
PATH=$PATH:$HOME/bin export PATH

5. Conclusion

To conclude, the shell requires its startup files for configuring the shell environment before actually using the environment.

In this article, we checked the various modes of shells. Then, we learned the significance of various Bash startup files. Lastly, we checked the differences between these startup files.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.