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
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
A few examples of editing .bashrc are provided below.
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
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
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 :
$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’.
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.
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.
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.
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.
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.
The changes made to bashrc file look like this:
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.
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.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Thank you. Exactly what I was hoping to find :-)
- dv1937