Bashrc is the file in your home directory that configures various settings in your Linux terminal. “Bash” – the shell in which you issue commands to your Linux server stands for “Bourne Again Shell” in honor of Stephen Bourne who first developed the original shell in the late 1970s. The “rc” in Bash stands for “Run Commands” or “Resource Configuration”. It contains a series of commands, most of which don’t matter to you as a user since a lot of it is automatic. But you can modify .bashrc in several ways that make a difference.
In this article, I’ll explain what .bashrc is, and how you can use it to vastly improve your Linux experience.
How to Find .bashrc
The .bashrc file is located in your home directory when you SSH into your Linux server. The dot (.) in front of it means that it’s a hidden file, and won’t show up by default when you issue the “ls” command. However, you can find it when you include the “all” flag as you can see here:
As you can see, when I first use “ls”, none of the files starting with a dot (.) are shown. When I use “ls -a”, it shows all the files including the hidden .bashrc.
How .bashrc is Executed
Contrary to what you might hear, .bashrc doesn’t necessarily run automatically whenever you open a new terminal session. Rather, it is called by files that do run automatically. Some of the files that can be run are:
- /etc/profile
~/.bash_profile
~/.bash_login
~/.profile
The first one is for all users, but the last three are different files for each user and run when that particular user initiates a new terminal session. One of these will contain the command to run .bashrc. For example, on my Ubuntu system, the “.profile” file contains the instructions to run .bashrc as you can see in the screenshot below:
So while .bashrc doesn’t need to be called, Linux systems include it in their automatically-run files so that it can perform initiation tasks for users.
Using .bashrc to Make Your Life Easier
Here are some useful ways to use .bashrc to streamline your workload and make things easier.
1. Creating an Alias for Commands
Notice how in the beginning, I had to use the following command to display hidden files:
ls -a
Let’s say this is a command you use frequently and want to save yourself the trouble of typing the parameters each time. Using .bashrc, you can include an alias for this command. For example, if I want to replace “ls -a” with “lh” (“h” for “hidden”), then I would add this command to .bashrc:
alias lh='ls -a'
When Linux executes .bashrc, it will automatically execute the above code. If you want to see how it works, you can just include it on the regular command line to see how it works:
As you can see, the command “lh” now displays all the files in the directory instead of excluding the hidden files including .bashrc.
This might seem like a simple example, but Linux admins execute complex commands all the time. We have our favorite format and ways of doing things, and the ability to create an alias to run a complex command with flags is an essential aspect of efficiency. This alone makes .bashrc worth it.
But there’s a lot more.
Changing the Color Scheme
Note in my above screenshots, that folders are displayed in cyan instead of dark blue. It used to be dark blue, but because the background is black, I could barely see the folder names. It used to look like this:
I don’t know who decided on the default colors in this bash program, but it’s suboptimal. I initially thought it might have been designed for a light background, but then the light-green colors would be hidden.
In any case, the solution is to use .bashrc to include a command to change the color of the directories to cyan. The command to do that is this:
export LS_COLORS=$LS_COLORS:'di=1;36:'
If you add this code to .bashrc, then the next time to start bash, directories will be in bright cyan instead of dark blue.
A related issue would be changing the color scheme in your editor of choice – like vi or vim. But Vim has its own “.vimrc” configuration file like .bashrc, and you should include the following command inside it to permanently change Vim’s color scheme:
colorscheme desert
In my experience, this is the best color scheme for dark backgrounds.
Adding Environment Variables to $PATH
One of the most common ways to customize your Bash experience is to include a folder containing all your favorite scripts in the $PATH variable so that Linux searches it when you issue a command. This saves you the trouble of manually specifying the path to the folder each time you want to access a command inside it.
The syntax to do this is as follows:
export PATH="$PATH:$HOME/bin"
Replace “$HOME/bin” with the path to the directory that you want to add. For me, once I run the above command, I can verify that it’s added to the global $PATH variable by running the following:
echo $PATH
This gives me the output as shown here:
As you can see, it’s added my home directory to $PATH. To make it permanent, add this command to .bashrc.
Changing the Command Prompt to Show Only the Current Directory
By default, my bash environment shows me my username in a format like this:
bhagwad@bhagwad:~$
However, what if you don’t want your username to appear like this? Maybe you want to share your screen on video or take screenshots for tutorials (like I’m doing right now), and you’re uncomfortable putting this information into the public sphere. In that case, it’s easy to modify the PS1 variable to change the prompt. Here’s the code to just display the path and nothing else:
export PS1='\w \$ '
Once you execute this, the command prompt now looks like so:
As before, to make this change permanent, ensure that you add this to .bashrc.
Applying the .bashrc Changes to the Current Session
It’s important to remember that simply making a change to .bashrc won’t automatically apply it to your current session. It applies to all future sessions. But who wants to exit and log in to the same session over and over again? The solution is to apply the changes to .bashrc to the existing session manually by using the following command:
source ~/.bashrc
Here’s a shorthand for the above command:
. ~/.bashrc
This command re-executes all commands in .bashrc, including the ones you just added. So you can see your changes immediately instead of waiting for the next session.
Conclusion
You can see from the above examples that .bashrc is a very useful file to customize your Linux experience. I use it myself for things like changing the color scheme, and you should too. If you work in the terminal a lot, you’ll quickly come to appreciate its power and it can make your entire Linux admin experience smoother.
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply