What is .bashrc file in Linux? – DigitalOcean

The .

Bashrc is a script file that runs when a user logs on. The file itself contains a number of settings for the terminal session. This includes setting or enabling: coloring, completing, shell history, command aliases, and more.

It is a hidden file and the simple ls command will not display the file.

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

$ ls -a

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

The . Bashrc has many comments that make it easy to understand.

To view the

bashrc file: $ cat .bashrc

Cat Bashrc

The following are some examples of editing

.bashrc. Defining Roles 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 terminal arguments.

Let’s define a function that says the date in a more descriptive way.

You will need to enter the .bashrc file in edit mode first.

$ 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 vi, press a colon (:) followed by ‘wq’ and enter.

Changes are saved. To reflect the changes in the base, exit and restart the terminal.

Or use the command

: $ source .bashrc

To run the newly created function 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 a parameter.

Our function will first use mkdir to create the directory with the name ‘directory_name’ and then cd in ‘directory_name’

.

Defining aliases in .bashrc Aliases

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

Alias 1 1

As a user, if there is an alias that you use regularly, 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 the wmi command and the terminal will run it as whoami

. In general, aliases can be

defined by adding the statement:

alias aliasname=’commands’

Here it is worth mentioning that there should be no space between ‘aliasname’, ‘=’ and ‘commands’

.

Aliases can also be used to store long paths to directories

.

Terminal customization

There are many ways to customize the terminal using the bashrc file. To change the

text displayed in the message, add the following line to the end of the

file: PS1=”JournalDev> ”

Save the edit and run :

$ source .bashrc

Once you update the bashrc file using the source command, its bash flag will change as the image below.

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

HISTSIZE is the number of commands stored in

  • memory when bash is executed. HISTFILESIZE is the number of commands stored on disk
  • .

Endnotes

The changes made to the

bashrc file look like this:

Redundant scripts can be placed in bashrc under one function. This will save a lot of time and effort. When editing the bashrc file, users should be careful and always make a backup before making any changes.

Contact US