Difference Between Bash Profile and Bashrc

Fumbani Banda Jan 30, 2023
  1. What Are Startup Files in Bash
  2. Difference Between ~/.bash_profile and ~/.bashrc in Bash
  3. Use ls Command to View Startup Files in Bash
Difference Between Bash Profile and Bashrc

This tutorial explains the difference between the ~./bash_profile and ~/.bashrc file in Bash.

What Are Startup Files in Bash

Startup files are files that are executed once the shell is started. The startup files depend on the type of shell that is started. A shell can be executed as an interactive shell or a non-interactive shell.

An interactive shell is a shell that reads commands from user input on the terminal and displays the output to the terminal. In contrast, a non-interactive shell is not associated with the terminal, such as running a script.

An interactive shell can further be divided into two. It can be an interactive login shell or an interactive non-login shell.

A login shell is the first process that is started after successfully logging in using /bin/login by reading the /etc/passwd file. The login shell executes under your user ID. A login shell is executed when you log in using the terminal, switch to another user, or use SSH.

The login shell starts a non-login shell. It can be a shell that starts with a process without login or starts from another shell. A process uses the name of the shell executable to start a non-login shell.

Difference Between ~/.bash_profile and ~/.bashrc in Bash

Once a shell starts as an interactive login shell, it reads the commands from ~/.profile, but a Bash reads them from ~/.bash_profile instead. Bash can only read the ~/.bash_profile, and if the file doesn’t exist, Bash reads the ~/.profile instead.

The ~/.profile is more commonly used in Linux distributions than ~/.bash_profile because it’s read and executed by all shells. In contrast, the ~/.bash_profile is only read and executed by Bash.

When the shell is started as an interactive non-login shell, it reads and executes the commands from the ~/.bashrc file.

The ~/.bash_profile and ~/.bashrc are located in the user’s home directory as dotfiles. Files in the user’s home directory are used to make configurations that only apply to that particular user.

These configurations usually override the system configurations. The ~/.bash_profile contains startup program configurations and user-specific environment configurations, while the ~/.bashrc file stores functions and aliases for a specific user.

The files located in the /etc/ directory are used to make system-wide configurations that apply to all users on the system. These files include /etc/profiles and /etc/bashrc.

Use ls Command to View Startup Files in Bash

To view the ~/.bash_profile or ~/profile and ~/.bashrc, navigate your home directory and type the command ls -al.

The -a option informs the ls command to list all files, including those starting with a dot. The -l option informs the ls command to list the files using a long list format.

The image below shows the ~/.bashrc and the ~/.profile files.

startup files

Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Linux Bash