
What is the .bashrc file?
Whenever a bash shell session starts it will reference bash configuration files such as .bashrc or .bash_profile and will load a user into their initial command prompt based off scripts placed in those files.
With the .bashrc file you’re able to customize aspects of a users bash sessions by adding functions or aliases for commands as well as customizing other aspects of a bash session such as output formats, colors ect.
The key part about making edits to your .bashrc file is that all changes will not only effect your current login shell but also any subshell (non-login shells) created during a shell session.
What is the bash_profile file?
Much like the .bashrc, setting environment variables to a users bash shell sessions can be done by making edits to a users .bash_profile as well.
When making edits to the .bash_profile file you are able to customize aspects of your shell sessions such as with functions, aliases, and more.
The only drawback of making edits to .bash_profile is that edits will only effect your login shell and not any subshells created during a session.
Difference between .bashrc vs .bash_profile
Both .bashrc and .bash_profile are bash startup files used for configuring our shell environments.
As mentioned previously between the two config files we’re able to setup environment variables for our bash shell which can simplify various tasks such as executing commands or executing scripts.
The main difference between .bashrc vs .bash_profile is that edits made to .bashrc will effect both login shell and subshells (non-login shells) and edits to .bash_profile only effect the login shell.
For example if you use utilities such as ‘screen’ on the command line interface, any changes made to .bash_profile would not carry over to those screen sessions since .bash_profile edits only effect your login shell.
If you create non-login shells on the command line interface and want your customizations to carry over you would want to make such edits to .bashrc instead.
File Locations
In all Linux distributions both configuration files for users are located in their home directories.
However on Ubuntu Linux you will see the .bash_profile referenced as .profile but will still be located in the user homedir locations:
~~~ Locations of a users .bashrc and .bash_profile files ~~~
~/.bashrc
/home/username/.bashrc
~/.bash_profile or ~/.profile (Ubuntu)
/home/username/.bash_profile or /home/username/.profile (Ubuntu)
How to create functions in .bashrc and .bash_profile
You can add functions into your .bashrc or .bash_profile to help save time when running certain commands.
Below we’ll explain how you can create a simple function for disk space investigations.
In this example we will be editing our .bashrc as we would prefer any changes to effect both login shell and non-login shells.
1. Craft the function
We’ll first start out with the command we’d like to create a function for and in this case it will be a ‘du’ command to check disk usage within our present working directory:
du -h --max-depth=1
Now to create this as a function we would first need to come up with a name for the function.
With that being said, lets just name our function disk_usage and write it out as follows:
function disk_usage
Now that we have those two pieces, lets put them together in a simple script:
function disk_usage() {
du -h --max-depth=1;
}
2. Add function to .bashrc or .bash_profile
Now we will open up the .bashrc or .bash_profile in our users home directory and add the following function to the bottom of the file, save, then exit the file:

3. Finalize our changes
We’ll need to run the ‘source’ command on our .bashrc or .bash_profile in shell prompt so our change will take effect in our current session.
If we do not use ‘source’ on our bash configuration files after making edits we will see no change take place in our current terminal session until we log back in to create a new session.
Run the following command in the users home directory (or specify the path):

Now that we have ran the ‘source’ command on our .bashrc we can test our work within the current shell.
4. Test our work
At this point we can test our function to see if it works properly:

As we can see now in our current shell, whenever we execute ‘disk_usage’ in the command prompt it will run the ‘du -h –max-depth=1’ command as our function so everything looks to be working properly.
How to create aliases in .bashrc and .bash_profile
Much like functions, creating aliases allows you to simplify commands you run on a normal basis as well.
As example if you wanted to your screen to ‘clear’ each time you enter in ‘c’ (to shorten it), you could add this into your .bashrc or .bash_profile.
Or if you want the ‘top’ command to execute when entering in just ‘t’ you could do that as well.
1. Craft the alias and add to .bashrc or .bash_profile file
In this example lets say we want to have ‘top’ execute whenever we enter in ‘t’.
To do so we would open up .bashrc or .bash_profile in our terminal window and add the following to the bottom of the file:
alias t='top'

2. Finalize our changes
We’ll need to ‘source’ our .bashrc or .bash_profile in shell prompt so our change will take effect during our current session otherwise we would need to open a new terminal window to start a new session.
If we do not use ‘source’ on our bash configuration files after making edits we will see no change take place in our current terminal session until we log back in creating a new bash instance.
Run the following command in the users home directory (or specify the path):
$ source .bashrc

Now that we have executed the ‘source’ command on our .bashrc we can test our work within the current shell and when you enter in ‘t’ to the command prompt you will be launched into the ‘top’ command.
Conclusion
Both .bashrc and .bash_profile are bash startup files used for configuring environment variables within a users bash sessions such as the prompt appearance, path variables, adding functions or even aliases for commonly used commands.
Making edits to either of these files can save a lot of time for Linux Administrators through the use of aliases and functions.
Within this article we covered the basics of how to edit environment variables such as adding functions or aliases to .bashrc and .bash_profile and also went over the key differences between the two.
Leave a Reply