What Is .bashrc and How to Customize It

A photograph of an Ubuntu terminal prompt.

There are a number of hidden files tucked away in your home directory. If you run macOS or a popular Linux distribution, you’ll see the “.bashrc” file near the top of your hidden files. This guide explains what the .bashrc file does and why you should edit and customize it.

What Is .bashrc?

If you run a Unix-based or Unix-like operating system, you likely have bash installed as your default terminal. While many different shells exist, bash is both the most common and, likely, the most popular. If you don’t know what that means, bash interprets your typed input in the Terminal program and runs commands based on your input. It allows for some degree of customization using scripting, which is where .bashrc comes in.

A screenshot of the bashrc file inside a file manager.

The .bashrc file contains a list of customization options for the bash shell and is loaded at each launch. The .bashrc file is found in the Home user directory. The . (dot) in front of the file name means that it is hidden from plain view. You will need to activate the “View Hidden Files” option to view it.

How Can I Edit .bashrc?

You can edit .bashrc in any text editor. You can also use nano to edit it in the Terminal.

nano ~/.bashrc

If you have not edited your .bashrc file before, you may find that it’s empty. That’s fine! If not, feel free to put your additions on any line.

Edits in .bashrc have to follow bash’s scripting format. If you don’t know how to script with bash, there are a number of online resources. This guide represents a beginner-friendly introduction into the aspects of Bash that are not mentioned here.

Any changes you make to .bashrc will be applied the next time you launch the terminal. If you want to apply them immediately, run the command below:

source ~/.bashrc

Note: you can add to .bashrc wherever you would like, but feel free to use comment (lines preceded by #) to document your code.

.bashrc Customization Tips

There are a couple of useful tricks to make your terminal experience more efficient and user-friendly.

1. Aliases

Aliases allow you to create a shorter version of a long command.

For example, the command ls is often used to display the contents of your directory. You can also use ls -lha to display the content with more detail. Now there is an alias ll, which is set to run ls -lha. Just type ll in the terminal, and it will run the ls -lha command.

A terminal showing a list of basic aliases.

You’ll need to format your aliases like so:

alias ll="ls -lha"

Type the text you want to replace on the left and the command on the right between quotes. Use this to create shorter versions of the command, guard against common typos, or force a command to always run with your favored flags. You can also circumvent annoying or easy-to-forget syntax with your own preferred shorthand.

2. Functions

In addition to shorthand command names, you can combine multiple commands into a single operation using bash functions. They can get pretty complicated, but they generally follow this syntax:

function_name () {
    command_1
    command_2
}

For example, the command below combines mkdir and cd. Typing md folder_name creates a directory named “folder_name” in your working directory and navigates there immediately.

md () {
    mkdir -p $1
    cd $1
}

The $1 you see in the function represents the first argument, which is the text you type immediately after the function name.

3. Adding a Simple Bash Prompt

You can also use the .bashrc file to create a custom shell prompt. The shell prompt is a line of text that shows up at the start of every terminal input line. This can contain either static information, such as your system’s name or dynamic scripts that change depending on the shell’s current working directory.

Here are several interesting bash prompts you can add to your .bashrc file.

4. Modify the PATH Variable

The PATH variable is an important variable that tells your shell where to find scripts and binaries for the current session. You can use the .bashrc file to store/change the content of your PATH variable. This is useful in cases where you want to run your own programs from a custom directory.

To modify the PATH variable, add the following line of code to the end of your .bashrc:

PATH="$PATH:"

This will tell your shell to load the default value of PATH before loading any custom arguments.

A screenshot showing the PATH variable referencing itself.

Now add your own directories to the end of this PATH variable.

PATH=":/home/$USER/bin:/home/$USER/git"
A screenshot showing a PATH variable with custom directories.

5. Exporting New Environment Variables

Environment variables are containers that hold session-specific values for a program or system process. It contains strings that give programs either an option or a resource that it can use while running.

For example, some programs use the “POSTGRESQL_DATABASE” environment variable when they link to an external database process:

POSTGRESQL_DATABASE="postgres://ramces:password@localhost:5432/db"
A screenshot showing a custom environment variable.

Export the variable to your shell. This will convert it from a regular shell variable to an environment variable:

POSTGRESQL_DATABASE="postgres://ramces:password@localhost:5432/db"
export POSTGRESQL_DATABASE
A screenshot showing the export command and the new environment variable.

6. Linking .bashrc with .bash_profile

The difference between .bashrc and .bash_profile is one of the biggest points of confusion for a novice Linux user. bash_profile only runs once during login, while .bashrc runs whenever a new shell starts.

However, this murky division means that it is possible to mix up the two and fail to run the command that you want to run. One quick way to solve this issue is by ensuring that .bashrc runs as soon as .bash_profile loads.

Add the following line of code to the end of the .bashrc file:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
A terminal showing a basic if statement in bash_profile.

This small block of code will tell the shell to check whether a .bashrc file exists in your home directory. If it finds one, Bash will load the file and run its commands in the current shell instance.

Good to know: while Bash is often used as a terminal interface for your system, you can also use Python to automate your computing tasks.

Frequently Asked Questions

Will my .bashrc file work with other Linux shells?

The developers of Bash designed the .bashrc file to work only with the Bash shell. However, it is important to note that most of the commands inside a .bashrc file are cross-compatible with some Unix-like shells. For example, you can copy the contents of a .bashrc file to its Korn shell counterpart, .kshrc, and it will still work.

Is it possible to set my default file permissions using Bash?

Yes. To do this, you need to add the umask command at the end of your .bashrc file. This is a simple program that sets the bit mask on the permission bits that you want to set. For example, the command umask 027 is the equivalent of running sudo chmod -R 750 ./directory.

Image credit: Unsplash. All alterations and screenshots by Ramces Red.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Ramces Red
Ramces Red - Staff Writer

Ramces is a technology writer that lived with computers all his life. A prolific reader and a student of Anthropology, he is an eccentric character that writes articles about Linux and anything *nix.