[ad_1]
If you spend a lot of time on the command line, chances are that you’ll want to customize your shell environment. This can mean creating aliases, adding a new directory to the $PATH
, or changing the look of the shell prompt.
You may have come across some tutorials where they say to put your configuration either in the .bashrc
, .bash_profile
or another configuration file that is read and executed by the bash shell.
In this article, we will talk about the Bash startup files and the difference between the .bashrc
and .bash_profile
files.
Interactive Login and Non-Login Shell #
When invoked, Bash reads and executes commands from a set of startup files. What files are read depends upon whether the shell is invoked as an interactive login or non-login shell.
A shell can be interactive or non-interactive.
In simple terms, an interactive shell is a shell that reads and writes to a user’s terminal, while a non-interactive shell is a shell that is not associated with a terminal, like when executing a script.
An interactive shell can be either login or non-login shell.
A login shell is invoked when a user login to the terminal either remotely via ssh or locally, or when Bash is launched with the --login
option. An interactive non-login shell is invoked from the login shell, such as when typing bash
in the shell prompt or when opening a new Gnome terminal tab.
Bash Startup Files #
When invoked as an interactive login shell, Bash looks for the /etc/profile
file, and if the file exists
, it runs the commands listed in the file. Then Bash searches for ~/.bash_profile
, ~/.bash_login
, and ~/.profile
files, in the listed order, and executes commands from the first readable file found.
When Bash is invoked as an interactive non-login shell, it reads and executes commands from ~/.bashrc
, if that file exists, and it is readable.
Difference Between .bashrc and .bash_profile #
.bash_profile
is read and executed when Bash is invoked as an interactive login shell, while .bashrc
is executed for an interactive non-login shell.
Use .bash_profile
to run commands that should run only once, such as customizing the $PATH
environment variable
.
Put the commands that should run every time you launch a new shell in the .bashrc
file. This include your aliases and functions
, custom prompts, history customizations
, and so on.
Typically, ~/.bash_profile
contains lines like below that source the .bashrc
file. This means each time you log in to the terminal, both files are read and executed.
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Most Linux distributions are using ~/.profile
instead of ~/.bash_profile
. The ~/.profile
file is read by all shells, while ~/.bash_profile
only by Bash.
If any startup file is not present on your system, you can create
it.
Conclusion #
.bash_profile
and .bashrc
are files containing shell commands that are run when Bash is invoked. .bash_profile
is read and executed on interactive login shells, while .bashrc
on non-login shells.
Check the Bash manual for more information about Bash startup files
.
If you have any questions or feedback, feel free to leave a comment.
[ad_2]
Source link