What Is the .bashrc File in Linux?

January 23, 2023


The .bashrc file is a configuration file for the Bash shell. The file consists of commands, functions, aliases, and scripts that execute every time a Bash session starts on Linux or macOS.

The file allows customizing the shell environment with various functionalities, shortcuts, and visual tweaks. Understanding the structure and function of the .bashrc file helps improve command line proficiency and comprehension of how the Bash shell works.

This article explains how to use and edit the .bashrc file with examples.

What is the .bashrc File in Linux?


  • Access to the terminal.
  • A text editor to edit the .bashrc file.

What is .bashrc?

.bashrc (short for bash read command) is a configuration file for the Bash shell environment. Every time an interactive Bash shell session starts, the .bashrc script file executes. The file contains various comments, configurations, and functions to customize the shell experience and automate tasks.

cat .bashrc terminal output

The .bashrc file is a startup shell script in a user's home directory. Other startup files, such as .profile or .bash_profile, help customize the Bash shell for specific users.

Note: See how .bashrc compares to .bash_profile for more details.

Why Should You Edit .bashrc File?

Editing and maintaining the .bashrc file comes with several benefits. Some reasons to edit and use the .bashrc file include the following:

  • Protection - The .bashrc file allows controlling variables, features, and commands related to security. System administrators can disable shell features for users through the .bashrc file.
  • Efficiency - Aliases and functions help automate repetitive tasks or create command shortcuts.
  • Accessibility - Environment variables and PATH customization create manageable ways to access programs.
  • Configurability - The Bash prompt is fully customizable. Change the look and feel by altering the terminal colors, prompt, or command outputs.
  • Portability - Since .bashrc is a script file, the configuration is easily transferred to another machine.

Editing the .bashrc file allows personalizing the Bash shell to make its use convenient and comfortable.

Defining Functions

A function is a set of commands defined under a single name. Functions can perform tasks independently or read variables and use their values to perform an action. The code in a function's body only executes once called (invoked).

Use functions to group multiple processes into a single command. Here are several examples:

  • Change directory and list files:
cdl() {
    cd $1; ls -lah

Call the function with the following:

cdl <directory>
cdl test terminal output

The $1 parameter replaces the directory provided in the command line, changes the directory with the cd command, then runs the ls command showing a list of all files and directories.

  • Create a new directory and navigate to it with the following function:
mkdircd() {
    mkdir $1; cd $1;

Call the function from the terminal with the following:

mkdircd <name>
mkdircd test terminal output

The mkdir command creates a new directory with the name provided in the command line, while the cd command enters the same directory immediately after creation.

extr() { 
    if [ -f $1 ] ; then 
      case $1 in 
        *.tar.bz2) tar xjf $1;; 
        *.tar.gz) tar xzf $1;; 
        *.bz2) bunzip2 $1;; 
        *.rar) unrar e $1;; 
        *.gz) gunzip $1;; 
        *.tar) tar xf $1;; 
        *.tbz2) tar xjf $1;; 
        *.tgz) tar xzf $1;; 
        *.zip) unzip $1;; 
        *.Z) uncompress $1;; 
        *.7z) 7z x $1;; 
        *)     echo "'$1' cannot be extracted via extract()" ;; 
         echo "'$1' is not a valid file" 

Call the function with the following syntax:

extr <filename>

The function utilizes the case and an if else statement to check the file type and use the correct extract command. Read more about functions and how they work in our Bash function guide.

Defining Aliases

An alias is a shortcut to a command. The alias definitions in a .bashrc file are permanent and always available for use.

Use aliases to create shortcuts for long commands or command combinations. Below are several practical examples:

  • Search through command history for a specific word:
alias hist = 'history | grep'

To use the alias, run:

hist <word>
hist systemctl terminal output

The alias lists the previously run commands with the history command and pipes the grep command to find the specified word.

  • Ping precisely five times and exit:
alias ping='ping -c 5'
ping count five terminal output

The ping command automatically links to the alias, and the -c 5 option becomes the default behavior.

alias update='sudo apt update && sudo apt upgrade'
update terminal output

Aliases help shorten a long list of linked commands into a much simpler form. For more information on aliases, check our alias command guide.

Customizing the Terminal

Customizing a terminal involves changing its look. Everything visible, such as the background color, text, and Bash prompt, is customizable.

Note: Check out our detailed guide to customize the Bash prompt.

How to Edit .bashrc File?

The .bashrc file location is in the user's home directory, and any text editor can read and edit its contents. To edit the .bashrc file, do the following:

1. Open the terminal (CTRL+Alt+T).

2. Backup the current .bashrc file:

cp ~/.bashrc ~/.bashrc.bak

The backup allows rolling back changes in case of any errors or problems.

3. Open the .bashrc file in a text editor. For example, if you use nano, run:

nano ~/.bashrc

Alternatively, if you use Vim, run the following command:

vim ~/.bashrc
default .bashrc file contents

The file contains some preset configurations and comments to explain what each section does.

4. Append new information at the end of the file. Add any functions, aliases, or customized features.

custom .bashrc functions

When ready, save the file and close.

5. Apply the changes by sourcing the .bashrc file:

source ~/.bashrc

The changes apply immediately and are permanent.

6. In case of any corruption, restore the backup:

cp ~/.bashrc.bak ~/.bashrc

The original .bashrc file returns.


After reading this guide about the .bashrc file, you know how easy it is to configure the terminal and add new functionalities.

Next, check out basic Linux commands all users should know, grab the free cheat sheet, and try to add some of the commands to your .bashrc file.

Was this article helpful?
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
bashrc vs. bash_profile: What Is the Difference?
September 22, 2022

The Bash shell holds custom information in hidden files such as .bashrc and .bash_profile. But what's the difference between the two? Find out when to use which file in this...
Read more
Bash Single vs. Double Quotes: What's the Difference?
January 5, 2023

Single and double quotes are often used in Bash to instruct the shell how to interpret the input string. This tutorial clarifies the difference between...
Read more
Bash Export Variable
December 30, 2021

All the variables the user defines inside a shell are local by default. It means that child processes of the shell do not inherit the values of the shell's variables. To make them available to child processes, the user...
Read more
How To Customize Bash Prompt in Linux
May 12, 2020

Follow this article to learn how to make changes to your BASH prompt. The guide shows how to edit the bashrc file, as well as how to modify PS1 variables. Use the information in...
Read more