What Is the .bashrc File in Linux?

What is the .bashrc File in Linux?


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.
  • AccessibilityEnvironment 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.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !