Tutorial

What is .bashrc file in Linux?

Published on August 3, 2022
author

Jayant Verma

What is .bashrc file in Linux?

The .bashrc file is a script file that’s executed when a user logs in. The file itself contains a series of configurations for the terminal session. This includes setting up or enabling: coloring, completion, shell history, command aliases, and more.

It is a hidden file and simple ls command won’t show the file.

To view hidden files, you can run the below command:

$ ls -a

Ls A Command

You can see the .bashrc command in the first column. The contents of .bashrc can be changed to define functions, command aliases, and customize the bash.

.bashrc file has a lot of comments that makes it easy to understand.

To view the bashrc file:

$ cat .bashrc

Cat Bashrc

A few examples of editing .bashrc are provided below.

Defining functions in bashrc

bashrc can be used to define functions that reduce redundant efforts. These functions can be a collection of basic commands. These functions can even use arguments from the terminal.

Let’s define a function that tells the date in a more descriptive manner.

First you’ll need to enter the .bashrc file in editing mode.

$ vi .bashrc 
Bashrc File
Bashrc File

This is what the terminal will look like. To start editing press any letter on the keyboard. At the end of the file add the following code:

today()
{
    echo This is a `date +"%A %d in %B of %Y (%r)"` return
}

Press escape. Then to save and exit from vi, press colon (:) followed by ‘wq’ and enter.

The changes are saved. To reflect the changes in the bash, either exit and launch the terminal again.

Or use the command:

$ source .bashrc 

To run the function just created call today :

$ today

Today

Let’s create another function. This would combine the process of creating a directory and then entering that directory into a single command.

In the bashrc file add:

mkcd ()
{
  mkdir -p -- "$1" && cd -P -- "$1"
}

This combines the two separate commands :

  • mkdir : creates a directory
  • cd : used to change the current directory

$1 represents the first parameter passed along with the function call.

To use this function:

$ mkcd directory_name

This command will pass ‘directory_name’ as the parameter.

Our function will first use mkdir to create the directory by the name ‘directory_name’ and then cd into ‘directory_name’.

Defining aliases in .bashrc

Aliases are different names for the same command. Consider them as shortcuts to a longer form command. The .bashrc file already has a set of predefined aliases.

Aliases 1 1

As a user, if there is an alias that you use regularly, then instead of defining it every time you open the terminal, you can save it in the .bashrc file.

For example, we can replace the whoami command with the following line of code.

alias wmi='whoami'

Don’t forget to save the edit and then run:

$ source .bashrc 

Now I can use wmi command and the terminal will run it as whoami.

Whoami 2

In general aliases can be defined by adding the statement:

alias aliasname='commands'

Here it is noteworthy to mention that there should be no space between ‘aliasname’, ‘=’ and ‘commands’.

Aliases can also be used to store lengthy paths to directories.

Customizing the terminal

There are a lot of ways to customize the terminal using bashrc file.

To change the text displayed at the prompt, add the following line at the end of the file :

PS1="JournalDev> "

Save the edit and run :

$ source .bashrc

Once you refresh the bashrc file using the source command, your bash prompt will change like the image below.

Changing Prompt

You can also change the limit of command history that is displayed when the UP arrow is pressed. To do so, change the HISTSIZE and HISTFILESIZE variables in the bashrc file.

Command History

  • HISTSIZE is the number of commands stored in the memory when bash is running.
  • HISTFILESIZE is the number of commands stored on the disc.

Ending notes

The changes made to bashrc file look like this:

Bashrc Changes

Redundant command sequences can be put in bashrc under a function. This will save a lot of time and effort. While editing the bashrc file, users should be careful and always take a backup before making any changes.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Jayant Verma

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 23, 2021

Thank you. Exactly what I was hoping to find :-)

- dv1937

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.