VPS Hosting

VPS Hosting

Buy Now

What Is the .bashrc File in Linux?

The .bashrc file is a shell script that runs every time a new Bash session is initiated. It is used to configure the Bash environment by defining aliases, functions, environment variables, and executing commands.

This file is located in the user’s home directory and is specific to each user account, understanding its purpose and can help customise the Linux command-line experience.

How to Edit the .bashrc File

  1. Using the text editor of your choice, open the file. Over the command line, you can run nano ~/.bashrc or vim ~/.bashrc.
  2. With that open in the text editor, you can add, modify, or remove lines as needed, ensuring correct syntax to avoid errors.
  3. To save your changes, if using Nano, press CTRL + X, then Y, and hit Enter. In Vim, press ESC, type :wq, and hit Enter.
  4. To apply those changes, you’ll need to run source ~/.bashrc or . ~/.bashrc.

Setting Environment Variables

  1. Open the file.
  2. Add the environment variable using the export command:
export PATH=$PATH:/path/to/your/directory
  1. Save and exit the text editor.
  2. Reload the file by running source ~/.bashrc.

Configuring the Command Prompt

  1. Open the file.
  2. Modify the PS1 variable to customise the prompt:
PS1="\u@\h:\w$ "
  1. Save and exit the text editor.
  2. Reload the file by running source ~/.bashrc.

Creating Aliases

  1. Open the file.
  2. Add aliases for frequently used commands:
alias ll='ls -la'
alias update='sudo apt update && sudo apt upgrade'
  1. Save and exit the text editor.
  2. Reload the file using source ~/.bashrc.

Creating Custom Functions

  1. Open the file.
  2. Add a function to automate a task:
mkdircd() {
  mkdir -p "$1"
  cd "$1"
}
  1. Save and exit the text editor.
  2. Reload the file using source ~/.bashrc.
  3. Use mkdircd new_folder to create and navigate into a directory.