What is .bashrc file in Linux?

Filed Under: UNIX/Linux
Bashrc File

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:

    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

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.


  1. dv1937 says:

    Thank you. Exactly what I was hoping to find 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content