What is the .bashrc file in Linux?

December 12, 2022

What is the .bashrc file in Linux?

This guide is part of the “Linux Commands” series. This series is focused on providing an in-depth overview of Linux commands and tools, in an easy-to-follow manner!

In Linux, the .bashrc file is a hidden file in your home directory that contains settings and configurations specific to the Bash shell. It is executed whenever you start a new Bash shell and can be used to set environment variables, define aliases, and customize your shell environment. The .bashrc file is a convenient place to store settings that you want to apply every time you use the Bash shell.

Because it is hidden by default, to view it you can use the ls -la command:

$ ls -la
total 24
drwxrwxr-x  6 user user 4096 Dec 12 22:34 .
drwxr-xr-x 24 root root 4096 Dec 12 21:40 ..
-rw-rw-r--  1 user user  220 Dec 12 21:40 .bash_logout
-rw-rw-r--  1 user user 3771 Dec 12 21:40 .bashrc
drwxrwxr-x  2 user user 4096 Dec 12 22:22 .cache
-rw-rw-r--  1 user user  655 Dec 12 21:40 .profile
drwxrwxr-x  2 user user 4096 Dec 12 22:34 .ssh

By default, the .bashrc file is located in the ~/.bashrc path, where ~ represents your home directory. For example, if your username is john, the .bashrc file would be located at /home/john/.bashrc.

If you open the file with nano .bashrc - you will see the following:

bashrc example

What are some of the things you can do with the .bashrc file?

  • Setting environment variables, such as PATH and SHELL, which are used by the shell and other programs to determine how to behave.
  • Defining aliases, which are shortcuts for commonly used commands. For example, you could define an alias for the ls command, such as ll, which would allow you to type ll instead of ls -la to list the files in a directory.
  • Customizing the appearance of the Bash prompt, by setting the PS1 variable to control the text and colors that are displayed.
  • Adding commands that should be executed every time you start a new Bash shell. This is useful for setting up your environment and running any initialization tasks that need to be done.
  • Enabling or disabling shell features, such as command history or tab completion.

So, now that we know some of the use cases let's look at a few examples.

A simple search function

We can use .bashrc to define a function that searches for a given text pattern in all files in the current directory and its subdirectories. This can save you time and effort when trying to find a specific piece of text in a large codebase.

# Define the 'grepdir' function

function grepdir() {
    # Search for the given pattern in all files in the current directory and its subdirectories
    grep -rnw . -e "$1"

You can now directly call grepdir keyword to search the current directory and sub-directories.

Setting up a git push alias

We can also write a function to set up a git alias that makes it easier to push changes to a remote Git repository. This can save you time by allowing you to use a shorter command to push your changes.

# Define an alias for the 'git push' command

alias gpush='git push origin $(git rev-parse --abbrev-ref HEAD)'

You can now use the gpush alias rather than writing git push origin every time.

Setting up general aliases

The .bashrc file is particularly useful for creating shorter aliases for common commands.

# Define an alias for the 'jupyter notebook' command

alias jn='jupyter-notebook'

You can now call your Jupyter workspace with just the jn command.

And you can do this for practically anything.