How to Use bashrc File in Linux

Written by: Nitish Singh   |   Last updated: June 28, 2023

1. Introduction

Are you looking to customize your Bash shell environment, define new commands, or set specific environment variables each time you open a new terminal? Look no further than the ~/.bashrc file!

2. What is .bashrc file ?

Technically, the .bashrc file is the configuration file for bash shell -- used in Linux and macOS. It stands for the bash read command. When you open a new bash shell the script inside this file is executed from top to bottom. Each time you open the terminal (new bash session) .bashrc file read and execute each command inside the file.

Purpose of the .bashrc file:

  • Customize the shell experience.
  • Setup command aliases.
  • Define tasks using functions
  • Define environment variables.
  • Setting command prompt appearance (PS1 variable).

Please note, changes made to the .bashrc file only apply to the current user. If you want to make system-wide changes, consider using the /etc/bash.bashrc file instead (which might be /etc/bashrc on some systems), but this requires administrative privileges and is generally not recommended unless necessary.

3. Finding and opening the .bashrc file

The .bashrc file is located in the user’s home directory. However, it is not easily visible. As you can see, a dot (.) is in the .bashrc file. That’s because it is a hidden file.

Generally, the regular user's home is /home/<username> and the root user's home directory is always /root. To change to a user home directory simply run cd ~username from the terminal

So, If you run the simple ls command, the terminal won’t show you the bashrc file. Instead, use ls -a command to list all files and directories including hidden files.

$ ls -a
using ls -a command  to list .bashrc file

Alternatively, you can use a regular expression .?* with ls command.

$ ls .?*
using ls .?* command to list .bashrc file

This lists files and directories that match the specified pattern of a single character followed by any number of characters.

As you can see, in both screenshots, the .bashrc file is present, along with other files.

You can run the following command to see what’s inside the .bashrc file.

$ cat .bashrc
see content inside .bashrc file

4. Editing the .bashrc file

To edit the .bashrc file, you can use any of your favorite text editors such vi/vim or nano.

Remember to be careful when making changes to your .bashrc file. If you make a mistake, it could cause problems the next time you open a terminal. It's a good idea to make a backup before making changes.

$ cp ~/.bashrc ~/.bashrc.bak

The command will create a .bashrc file backup, naming it .bashrc.bak

Now to edit the .bashrc file, you can choose the text editor of your choice.

For example, you can use the nano command to edit the file. To do so, run the following command.

$ nano ~/.bashrc

5. Using .bashrc File with some Examples

In this section, we will explain common se cases of .bashrc file with examples. Here we have setup up functions, handy command aliases, and added some color to your terminal prompt.

5.1. Function

In the .bashrc file, you can define a function just like you would in a bash script. Rather than typing a long command, you can create a function in .bashrc so that it handles the task for you.

Here's an example of a simple function to find a file:

findfile() {
find . -name "$1"
}

You add the code at the end of the .bashrc file.

add the findfile function in the .bashrc file

After you add this function to your .bashrc file, you need to run source ~/.bashrc in your terminal for the changes to take effect. After that, the findfile function will be available in all your new terminal sessions.

$ findfile fruits.txt
run findfile function from the terminal

5.2. Setting up Aliases

Aliases are an intelligent way to shorten a command for everyday use. For example, if you are a network administrator and use the ping function to check the response of a particular IP address, then you need to run the following command.

$ ping -c 5 google.com

To shorten it, you can create an alias and name it according to your preference. For example, you can shorten it to “ping” or “ping5” -- the choice is yours.

Open the .bashrc file and write the following command to set it up.

alias ping = ‘ping -c 5’

And, just like that, you can create an alias for any command you want. 

One of the common aliases you can use is to shorten $ sudo apt update && sudo apt upgrade into a single-word command.

alias update = “sudo apt update && sudo apt upgrade”

So, next time, you have to type the update command to run the whole command.

If the alias didn’t work and you get the command error not found, your Linux environment has another file that lets you load them. If you scroll down, you’ll find the following line of code.

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

You’ll need to put the aliases in the .bash_aliases file. This is incredibly handy as it keeps your aliases in separate files.

So, open up the .bash_aliases file and copy-paste the following code.

alias ping5 = 'ping -c 5'
alias update = 'sudo apt update && sudo apt upgrade'
alias ll = 'ls -a1F'
alias la = 'ls -A'
alias l = 'ls -CF'
alias wmi = 'whoami'
adding aliases to bashrc file

5.3. Setting Command Prompt

You can custom code to your .bashrc to customize your command prompt.

Here is an example to customize your shell prompt by defining color variables. Then setting PS1, which is the primary prompt string.

Paste the following lines to the end of .bashrc file.

# colors!
red="\[\e[38;5;203m\]"
green="\[\e[38;5;38m\]"
reset="\[\033[0m\]"

# '\u' adds the current user name to prompt
# '\W' adds the current directory name
export PS1="${red}\u${green} \W\n\$ ${reset}"
adding custom command prompt in bashrc file by setting PS1 variable including some colors

Save the file and source the .bashrc file.

customized command prompt

Frequently Asked Questions

Difference between .bashrc and  .bash_profile file?

Both .bashrc and .bash_profile file exists in user's home directory. Both are used to customize your shell experience. The difference comes from the way they are loaded.

.bashrc file is read each time a user opens a new terminal. That means its executed whenever a new non-login interactive shell is started. It's typically used to set environment variables, define functions, and configure aliases

Whereas .bash_profile file is read when you login to the system via command line such as ssh into a remote machine. Normally used to load scripts or specific environment variables for login sessions. If you want you can configure .bash_profile file to source .bashrc file so customization applies for both login and non-login shells.

Should do PATH adjustments in .bashrc or .bash_profile?

Using the .bash_profile file to adjust the PATH variable is generally suggested. It ensures the changes are applied at login and not when opening a terminal. This also provides proper PATH in all interactive shell sessions. However, you can also configure them in .bashrc, as it’ll have the same effect. 

About The Author

Nitish Singh

Nitish Singh

Nitish Singh is a certified C1 Advanced(CEFR) tech writer from Kolkata, India. He has a proven track record of making tech accessible to everyone, with his work being read by over a million users worldwide. With a master’s degree in computer science and a history of research and publication, he has worked in various roles, such as content manager, news writer, blockchain writer, and Linux enthusiast. He loves Linux to its kernel and learns new Linux concepts daily.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment