The more time you’ll spend on the terminal, the more you’ll realise its power and the scope of customisability it has. Tailoring your terminal to exactly how you work with it can go a long way in increasing your productivity.
Many tutorials on customising terminals or shells might point you towards either the .bashrc or the .bash_profile configuration files. The bash shell reads these files, and they dictate how it functions.
In this article, we’re going over Bash startup files and the difference between the aforementioned configuration files, so you know where to make your customisations.
Also read: Bash functions explained
Interactive login and non-login shells
When you invoke Bash, it reads and executes commands from a given set of startup files. Now, which files the shell reads depends on whether or not the shell is login or non-login.
An interactive shell reads and writes to and from a user’s terminal. On the other hand, a non-interactive shell has no association with the terminal whatsoever, much like when you’re executing a script.
Now, these shells can be either login or non-login as well. When a login shell is invoked, the user will have to log in to the terminal either locally or remotely (via SSH or any other method). Alternatively, you could launch Bash with the –login flag.
An interactive non-login shell is spawned from a login shell. For example, when you invoke Bash by typing in the terminal itself or opening a new Gnome terminal tab.
Also read: How to create Bash aliases?
Startup files in Bash
When you invoke an interactive login shell, bash will look for the /etc/profile file and depending on whether or not the file exists, the command listed in the said file will be executed.
Afterwards, Bash search for the ~/.bash_profile, ~/.bash_login and ~/.profile files in that order and executes commands from the same. However, when Bash is invoked from an interactive non-login shell, all commands are read and executed from the ~/.bashrc file provided it exists and is readable.
Also read: How to check if a file or directory exists in Bash?
What’s the difference between .bashrc and .bash_profile?
The .bash_profile file is read and executed when you invoke Bash from an interactive login shell. On the other hand, .bashrc is executed when for an interactive non-login shell.
Keeping this in mind, you should only use bash_profile to run commands that need to run only once. This could be a slight change in the terminal or customising the $PATH environment variable.
Any commands that need to run recurringly each time you launch a new shell go in the .bashrc file. This also includes your aliases, functions, custom prompts and history customisations.
Generally speaking, the ~/.bash_profile file sources the .bashrc file, meaning each time you log in to your terminal, both files are read and executed. However, most Linux distros use the ~/.profile file instead of the ~/.bash_profile because all shells read the former while Bash only reads the latter.
Also read: How to concatenate strings in Bash?