.bashrc vs .bash_profile

Introduction

You'll probably want to customize your shell environment if you spend a lot of time on the command line. Creating aliases, adding a new directory to the $PATH, or modifying the shell prompt's appearance are all examples of this.

The .bashrc is for configuring interactive shell sessions, while .bash_profile is primarily for setting up the initial environment for login shells.

Some guides recommend putting your configuration in the .bashrc, .bash profile, or another configuration file that the bash shell reads and executes.

In this tutorial, the .bashrc and .bash_profile files will be discussed along with the differences between them. We will also address a few FAQs on the .bashrc and .bash_profile files.

Interactive Login and Non-Login Shell

Bash reads and executes commands from a series of startup files. Whether the shell is invoked as an interactive login or non-login shell determines which files are read.

It is possible for a shell to be interactive or non-interactive.

An interactive shell, in simple words, is one that reads and writes to a user's terminal, whereas a non-interactive shell is one that is not associated with a terminal, such as when running a script.

A login shell or a non-login shell are both examples of interactive shells.

When a user logs in to the terminal, either remotely via ssh or locally, or when Bash is run with the --login option, a login shell is launched. When you type bash at the shell prompt or open a new Gnome terminal tab, the login shell launches an interactive non-login shell.

Bash Startup Files

When used as an interactive login shell, Bash searches for the /etc/profile file and executes the commands listed in it if it exists. Then, in the order listed, Bash searches for ~/.bash_profile, ~/.bash_login, and ~/.profile files and executes commands from the first readable file found.

If /.bashrc exists and is readable, Bash reads and executes commands from that file when used as an interactive non-login shell.

Difference Between .bashrc and .bash_profile

When Bash is used as an interactive login shell, the .bash_profile file is read and executed, however, when Bash is used as an interactive non-login shell, the .bashrc file is read and executed.

To run commands that should only be run once, such as changing the $PATH environment variable, use .bash profile.

In the .bashrc file, put the commands that should run every time you start a new shell. This includes things like aliases and functions, custom prompts, and history tweaks.

~/.bash_profile usually contains lines like the ones below, which reference the .bashrc file. This implies that both files are read and run every time you log in to the terminal.

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

Instead of /.bash_profile, most Linux distributions use /.profile. All shells read the /.profile file, but only Bash reads the /.bash_profile file.

You can generate a startup file if one does not exist on your system.

FAQs on the  .bashrc and .bash_profile

Can I use .bashrc and .bash_profile interchangeably?

No, they serve different purposes. .bashrc is sourced for each new interactive shell, while .bash_profile is sourced only for login shells. It is common to include a reference to .bashrc within .bash_profile to ensure both are loaded.

How can I edit or create a .bashrc or .bash_profile file?

You can edit or create these files using a text editor like nano or vim. The files are typically located in the user's home directory. For instance, ~/.bashrc and ~/.bash_profile.

Do changes made in .bashrc or .bash_profile take effect immediately?

Changes made in these files will only take effect when a new shell session is started or when the file is sourced using the "source" command. You can also run exec bash to start a new shell session and apply the changes immediately.

What kind of settings can be configured in .bashrc or .bash_profile?

In these files, you can configure environment variables, set aliases, define functions, modify the PATH variable, and customize your shell prompt, among other shell-specific settings.

Can I use conditionals or loops in .bashrc or .bash_profile?

Yes, you can use conditionals (if statements) and loops (for or while loops) in these files to perform conditional initialization or execute specific commands based on certain conditions.

Are .bashrc and .bash_profile limited to Bash shell only?

Yes, these files are specific to the Bash shell and are not executed by other shell types such as Zsh or Fish. Other shells have their own equivalent files for similar purposes.

What happens if both .bashrc and .bash_profile exist?

If both files exist, .bash_profile takes precedence for login shells. However, it's common practice to source .bashrc within .bash_profile to ensure the settings from both files are loaded for a login shell.

Conclusion

The files .bash_profile and .bashrc contain shell commands that are executed when Bash is called. On interactive login shells, .bash_profile is read and executed, but on non-login shells, .bashrc is read and executed.

More information on Bash startup files may be found in the Bash manual.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.