Edit and Reload the .Bashrc File

  1. What Is .bashrc?
  2. Edit .bashrc and Reload Changes

This tutorial demonstrates editing the .bashrc file and reloading the new changes by using the source command or the exec command.

What Is .bashrc?

.bashrc is a bash shell script that bash runs whenever it starts interactively. It initializes an interactive shell session. The .bashrc file contains configurations for the terminal session. These configurations include coloring, shell history, completion, command aliases, environment variables, and many more.

The .bashrc is a hidden file. To view hidden files, run ls with the -a option. The -a option tells ls to list all entries, including those starting with ., the -l option tells ls to list the entries in a long list format, and the | pipes the ls output to the head command which prints the first ten lines of the output.

$ ls -al | head

From the output below, we can observe that we have the .bashrc file.

total 94064
drwxr-xr-x 1 fumba fumba     4096 Nov 14 11:37 .
drwxr-xr-x 1 root  root      4096 Sep  7 07:41 ..
-rw------- 1 fumba fumba    30965 Nov 13 23:16 .bash_history
-rw-r--r-- 1 fumba fumba      220 Sep  7 07:41 .bash_logout
-rw-r--r-- 1 fumba fumba     3771 Sep  7 07:41 .bashrc
drwxr-xr-x 1 fumba fumba     4096 Sep  7 21:35 .cache
drwx------ 1 fumba fumba     4096 Sep  7 15:05 .config
drwxr-xr-x 1 fumba fumba     4096 Sep  7 07:41 .landscape
drwxr-xr-x 1 fumba fumba     4096 Sep 23 06:41 .local

We can use the cat command to display the content of the .bashrc file by typing the following command.

$ cat .bashrc

Edit .bashrc and Reload Changes

Add the following function at the end of the .bashrc file using your preferred text editor. The function displays the date of that particular day when it is called.

date_today(){
    date '+Today is %A, %B %d, %Y.'
}

After saving the changes, we can reload the .bashrc to reflect the new changes by running the command below. The source command reads and executes the content of the .bashrc file.

$ source .bashrc

Another way to reload the changes in the .bashrc file is by running exec bash. The exec bash command replaces the current bash shell with a new instance.

$ exec bash

To call the function we created in the .bashrc file, type the function’s name as shown below.

$ date_today

The output of the function above prints the current date.

Today is Sunday, November 14, 2021.
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Bash Exec