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.
- 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:
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:
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 -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
- Launches Bash with the
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
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.
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.
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:
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.
After reading this guide, you know the main differences between .bashrc and .bash_profile.
Next, learn how to customize the Bash prompt in Linux.