bashrc vs. bash_profile: What Is the Difference?

September 22, 2022

Introduction

The .bashrc and .bash_profile startup files help customize the Unix command-line environment. The configuration files hold useful custom information, such as PATH directories, command aliases, and custom styles.

Knowing how Bash reads these two files helps distinguish what configuration should go in which file.

This article explains the difference between .bashrc and .bash_profile startup files.

bashrc vs. bash_profile: What Is The Difference?

Prerequisites

  • Access to the command line/terminal.
  • A text editor to create and edit files.
  • A user account with sudo privileges.

Interactive and Non-Interactive Shells

A shell has two working modes:

  • An interactive shell reads user input typed on a keyboard. Commands run in the current shell and the system waits for further instructions from the user. A Bash shell is an interactive shell.
  • A non-interactive shell executes commands read from a file in a new subshell. The shell executes commands from the file and exits. Init, startup, and executed shell scripts start non-interactive shells.

How you run a Bash script determines the working mode. To demonstrate, do the following:

1. Create a Bash script file (test.sh) and add the following code:

#!/bin/bash

[[ $- == *i* ]] && echo Interactive || echo Non-interactive

The code checks whether the current shell $- contains the -i option (*i*) or not. If it does, the shell is interactive.

2. Make the script executable:

chmod +x test.sh

3. Execute the script with:

./test.sh
test.sh script non interactive terminal output

The output shows the shell is Non-interactive. Executing a script opens a new subshell, executes the command, and returns to the parent shell.

4. As a comparison, source the script with:

. test.sh
test.sh script interactive terminal output

The output prints the shell is Interactive. The commands run in the current environment.

Bash Startup Files

Bash executes different startup files depending on whether the shell is a login or non-login shell. To check the current shell type, use the shopt command:

shopt -q login_shell && echo 'Login shell' || echo 'Non-login shell'

The output prints Login shell if the user:

  • Logs in from the terminal remotely (for example, via SSH).
  • Logs in from the terminal locally (for example, using the login command).
  • Launches Bash with the -l option (bash -l).
bash login shell terminal output

On the other hand, the output prints Non-login shell when the user:

  • First opens the terminal.
  • Opens a new terminal tab.
  • Launches Bash from a login shell with the bash command.
bash non-login shell terminal output

Which startup files Bash reads from depends on whether the current shell is login or non-login.

Note: On Linux, the default starting terminal is an interactive non-login shell. On macOS, the default is an interactive login shell.

.bashrc

The .bashrc file is a hidden script file containing various terminal session configurations. The file executes automatically in both interactive and non-interactive non-login shells.

When running a non-login shell (subshell), the primary read configuration file is the /etc/bash.bashrc. The file contains system-wide configurations for non-login shells.

Note: In RHEL, system-wide configurations for non-login shells are in /etc/bashrc.

After, the shell searches for the ~/.bashrc configuration file for the specific user.

.bash_profile

The .bash_profile file is a hidden script file with custom configurations for a user terminal session. The file automatically executes in Bash interactive login shells.

When running an interactive login shell, the system reads the following configuration file first:

  • /etc/profile - Stores global configurations for login shells. The configurations apply to all users.

Next, the Bash shell searches for specific user configuration files in the following order:

  • ~/.bash_profile
  • ~/.bash_login
  • ~/.profile

The first found file is read and executed.

Note: Most Linux distributions have the .profile configuration file set up because it's read by any shell type, including Bash.

Add the following code to the .bash_profile file to force reading .bashrc in an interactive login shell session:

if [ -f ~/.bashrc ]; then
	. ~/.bashrc
fi

Difference Between .bashrc and .bash_profile

The critical differences between .bashrc and .bash_profile are:

  • .bashrc defines the settings for a user when running a subshell. Add custom configurations to this file to make parameters available in subshells for a specific user.
  • .bash_profile defines the settings for a user when running a login shell. Add custom configurations to this file to make parameters available to a specific user when running a login shell.

Conclusion

After reading this guide, you know the main differences between .bashrc and .bash_profile.

Next, learn how to customize the Bash prompt in Linux.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP who is passionate about programming. Her background in Electrical Engineering and Computing combined with her teaching experience give her the ability to easily explain complex technical concepts through her content.
Next you should read
How To Customize Bash Prompt in Linux
May 12, 2020

Follow this article to learn how to make changes to your BASH prompt. The guide shows how to edit the bashrc file, as well as how to modify PS1 variables. Use...
Read more
14 Dangerous Linux Terminal Commands
November 17, 2021

It is always dangerous to run a Linux terminal command when you aren't sure what it does. This article lists 14 Linux commands that can have adverse effects on your...
Read more
Bash Math Operations (Bash Arithmetic) Explained
April 14, 2022

You can perform math and arithmetic operations in Bash directly. Although the functionalities are...
Read more
Bash HereDoc Tutorial With Examples
March 3, 2022

A HereDoc helps pass multiple commands to another command, making redirection and Bash scripts easier to...
Read more
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.