Linux, Unix, and MacOS operating systems provide the bash shell in order to provide a command-line environment and shell. The bash shell has different configurations which can be configured using the .bashrc
and .bash_profile
files. The .bash and .bash_profile files can be used to set different bash configurations like $PATH, history, alias, command prompt, etc. But why there are two different bash configuration files named .bashrc
and .bash_profile
.
.bashrc File Location
The .bashrc files are related to the user-level bash shell and script configuration. This means the .bashrc files are located in the user’s home directory. The home directory can be specified with the ~
in-bash or Linux distributions. The following path can be used to locate .bashrc file.
~/.bashrc
Alternatively, if we are aware of the current user home directory we can specify the .bashrc file location using the current user home directory path.
/home/ismail/.bashrc
.bashrc File
The .bashrc file is located under every user’s home directory. The .bashrc file location can be expressed like ~/.bashrc
. The .bashrc file is executed when a non-login bash shell is started. A typical .bashrc file is like the below which sets some configuration like the below.
.bash_profile File
The .bash_profile
file is executed for the login shells. When a user logins using his username or password the .bash_profile file is executed in order to configure the bash shell. The .bash_profile file is generally used by the MacOS operating systems but can be also used in Linux and Unix. But the Linux operating systems generally do not provide the .bash_profile by default.
.bashrc vs .bash_profile
The .bash_profile is called only for interactive logins using a username which generally occurs during ssh, GUI, or terminal login. The .bashrc is called every time a new shell is opened. This happens a lot of times after a user logins to his account. The .bashrc is not called during user login by default.
Topic | .bashrc | .bash_profile |
---|---|---|
Run At Shell Logins | Yes | No |
Run At New Shell Start | No | Yes |
Location/Path | Users’ home directory | Users’ home directory |
Call .bash_profile From .bashrc File
As the .bash_profile is not called only during user login by default we may need to use the .bash_profile file for the user’s new shells. The most popular way is using the .bash_profile file. The following code snippet can be used to run .bash_profile via the .bashrc file.
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
.bashrc Comment
The .bashrc file may contain a lot of statements. We can comment on these statements to explain detail. The comments
are used in .bashrc to explain the script or statements. The #
sign is used at the start of the line to make a line comment. The comment lines are not executed by bash.
#This is a comment
export PS1= "$ "
#This line is comment too.
.bash_profile Comment
Similar to the .bashrc the .bash_profile may contain comments to explain code or statements. The #
is used to create comments and put them at the start of the line.
#This is a comment
export PS1= "$ "
#This line is comment too.