What is .bashrc File in Linux

Published on October 25, 2024

Linux has several configuration files. For instance, there are script files that run when you boot up to help with configuring different items, such as your terminal sessions to make it convenient to use.

When talking about .bashrc, it’s an example of such configuration files and it’s role is to define configurations for your terminal session. For instance, the .bashrc defines different aliases that you can use within your terminal. Moreover, changing the look of your terminal is also done within the .bashrc. Read on as this post discusses more about what the .bashrc file in Linux is.

What is .bashrc and When Should You Edit It?

The .bashrc stands for bash read command. It is one of the Linux configuration files that defines how the Bash shell environment will behave when you log in to your system. Anytime you open your Bash shell, the .bashrc script will execute to accommodate all the specifications defined in it, such as the color for the shell.

The .bashrc contains all the functions and other configurations required to correctly use the shell. Moreover, you have room to add any configurations depending on how you want to tune your shell experience.

Notably, the .bashrc is one of the hidden files in Linux and a simple ls command won’t find the file. However, you can view the hidden files while in your home directory by adding the -a flag. Better yet, add the grep command to filter the results for better experience. For instance, running the below command will now display the .bashrc file.

Copy

You can then view the contents of the .bashrc file by opening it with any text editor or command. For this example, the cat command displays the contents of the .bashrc file on our terminal. You will notice that  the file is a script defining the different functions and configurations for each Bash shell session.

So, when should you edit your Linux .bashrc file? First, you can edit the .bashrc when you want to create an alias for different commands for easy automating of repetitive tasks.

Still, you can edit the .bashrc when you want to customize the appearance of your terminal. With the script, you can change the colors, prompt, and feel of the terminal with simple steps. Moreover, you can create functions to ease different tasks, such as creating hierarchical directories. Ideally, how you use the .bashrc will depend on your goal.

Examples of How to Use the .bashrc File in Linux

When you want to edit the .bashrc file, you must first open it with super user privileges. Once it opens, you can comment out, add new configurations, or create functions to meet your intended use. To open the .bashrc file, we will use the nano text editor. You can still use others, such as vim.

For our first example, let’s create an alias for the ‘ls -l’ command. Instead of simply listing the directory content’s, let’s set the ls command to long-list the contents.

To do so, add the “alias ls =’ls -l’” line in your .bashrc file, as in the below image.

Next, save the file and exit it.  Although we’ve already made changes to our .bashrc file, the changes won’t apply until we source the file. Notice how if we run the ls command, we don’t get the long-listing format.

However, after we run the source .bashrc command, we will get different results when we re-run the ls command. We’ve managed to create an ls alias.

We can also have another example where we are changing the look and feel of our Bash shell session by setting a different prompt. Instead of showing the hostname and username as the prompt, adding the below line will set the text as the terminal prompt.

Copy

Once again, save the file, and source the .bashrc. Once you do so, you will immediately notice that you now have a different prompt for your terminal.

That’s how you can use the .bashrc file to achieve different goals.

Conclusion

The .bashrc file in Linux is a hidden file that defines the configurations for the Bash shell session when you boot up.  You can edit the script file to set aliases, new functions, or define other configurations to make your work easier. This post discusses the .bashrc file in detail and gives example of how to edit it  for different purposes.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price