VPS Hosting

VPS Hosting

Buy Now

What is the .bashrc file? And how to use it

.bashrc is a script that runs whenever a new Bash session is started. Its purpose is to configure the Bash environment by defining aliases, functions, environment variables, and executing commands. This file is located in a user’s home directory, as it is specific to each individual account.

In this guide, you’ll learn what .bashrc is and how it can be used to customize your command-line experience.

How to edit .bashrc

Before you can make changes to bash you need to know the basics of how to open it and save any changes, in this short run through we’ll be using nano and vim which are command line text editors.

1. First off, using your preferred text editor open .bashrc below are two examples for doing that with nano and vim.

nano ~/.bashrc
vim ~/.bashrc

2. Moving on with the file open you can, make, modify or remove lines as you require (ensuring correct syntax) these are the customisations you’ll be making to .bashrc. Alias’ are a great example and can really aide your overall productivity. The examples below will make an alias ll command that runs ls -la and update which runs apt update and upgrade.

alias ll="ls -la"
alias update='sudo apt update && sudo apt upgrade'

3. After you’ve made your changes, make sure you save them using nano you can do this with CTRL+X & then hitting Y.

4. To complete the customisation process you’ll need to run the .bashrc file you can usually do that with.

source ~/.bashrc

or

~/.bashrc

More customisation examples

Below are some more examples of how you can use the .bashrc file to customise your command line experience and improve your workflow.

Setting Environment Variables

Add the environment variable using the export command.

export PATH=$PATH:/path/to/your/directory
Configuring the Command Prompt

Modify the PS1 variable to customise the prompt.

PS1="\u@\h:\w$ "
Creating Custom Functions

Below is a custom function that helps automate a task, once added you can use mkdircd new_folder to create and navigate into a directory.

mkdircd() {
  mkdir -p "$1"
  cd "$1"
}