Report this

What is the reason for this report?

What Is .bashrc in Linux? Configuration Explained

Updated on June 23, 2025
Jayant VermaManikandan Kurup

By Jayant Verma and Manikandan Kurup

What Is .bashrc in Linux? Configuration Explained

If you have ever worked with a Linux terminal, chances are you have come across the .bashrc file. This powerful script is the key to personalizing your command-line environment and making it work more efficiently for you.

In this article, we will walk you through what this file is, where to find it, and how to edit it safely. You will learn practical skills like creating time-saving command aliases, writing powerful shell functions, and customizing your terminal prompt’s appearance. Finally, we’ll cover essential best practices and common mistakes to help you build a more productive and powerful command-line workflow.

Key Takeaways

  • The .bashrc file is your personal script that automatically configures your environment every time you open a new terminal window.
  • Its primary purpose is to add efficiency by creating time-saving command aliases, powerful shell functions, and a custom prompt.
  • Always back up your file before editing and use the source ~/.bashrc command to apply any changes you make to the current session.
  • Use an alias for a simple command nickname, but switch to a function when you need to handle arguments, logic, or multiple steps.
  • Use .bashrc for settings you want in every new terminal (like aliases), and use .bash_profile for settings that only need to run once at login.

What is a .bashrc file?

The .bashrc file is a shell script that the Bash shell runs whenever it is started interactively. In simple terms, every time you open a new terminal window, Bash reads and executes the commands within this file. This makes it the perfect place for your personal Linux environment configuration.

It allows you to store and automatically apply:

  • Command aliases: Shortcuts for your most-used commands.
  • Shell functions: More complex, custom commands that can accept arguments.
  • Custom prompts: Change the look and feel of your command prompt.
  • Environment variables: Set paths and configurations for other programs.

It’s a hidden file located in your user’s home directory (~/), which is why a simple ls command won’t display it.

How Bash executes configuration files?

When you start a Bash session, it doesn’t just randomly look for .bashrc. The Bash shell follows a specific sequence to determine which configuration files to load. This logic depends on whether the shell is a login or non-login shell, and if it’s interactive or non-interactive.

  • Interactive login shell: (e.g., connecting via SSH) Bash looks for /etc/profile first, then searches for ~/.bash_profile, ~/.bash_login, and ~/.profile in that order. It only reads and executes the first one it finds.
  • Interactive non-login shell: (e.g., opening a new terminal window on your desktop) Bash reads and executes ~/.bashrc. This is the most common scenario for desktop users.

Crucially, most distributions’ ~/.bash_profile or ~/.profile files contain a small script that explicitly checks for and runs ~/.bashrc. This ensures that your .bashrc settings are loaded even in a login shell, unifying your environment.

A major point of confusion is the .bashrc vs .bash_profile debate. Let’s clarify the roles of the main configuration files:

File Name Scope When It’s Executed Common Use Cases
/etc/bash.bashrc System-wide For every user’s interactive, non-login shell Set default aliases and functions for all users on the system.
~/.bashrc User-specific For a user’s interactive, non-login shells The main file for personal aliases, functions, and prompt customizations.
~/.bash_profile User-specific For a user’s login shell Set environment variables and run commands that only need to happen once per session.
~/.profile User-specific Fallback for ~/.bash_profile A more generic version that can be used by other shells, not just Bash.

For your day-to-day terminal customizations like aliases and prompt settings, ~/.bashrc is the correct file to edit.

Where to find and open the .bashrc file in Linux?

The Linux .bashrc file is mostly located in your user’s home directory. You can find and open it from the command line.

To view the file, use ls -a in your home directory to see all the hidden files.

Ls A Command

To open the .bashrc file in your Ubuntu terminal (or any other Linux distro), you can use a text editor like nano or vi.

  1. nano ~/.bashrc

In some minimal installations, a .bashrc file might not exist. If you run ls -a and don’t see it, you can simply create it with the touch command:

  1. touch ~/.bashrc

Now you can open the empty file and start adding your configurations.

How to safely edit .bashrc?

Before you make any changes, you must create a backup. A simple syntax error in your .bashrc can prevent your terminal from starting correctly.

The first step is to create a backup:

  1. cp ~/.bashrc ~/.bashrc.bak

If you ever run into trouble, you can just restore this backup.

Now you can start editing the file. Open it with your preferred editor to add your changes. We’ll look at some practical examples in Practical .bashrc examples.

Once you save your edits, they won’t take effect immediately. For this, you must reload the configuration using the source command.

  1. source ~/.bashrc

This command reads and executes the file in the current shell session. It’s the standard way to apply .bashrc changes without interrupting your workflow.

Let’s look at few practical examples by editing the .bashrc file:

Practical .bashrc examples

Let’s see how you can utilize the .bashrc file to customize your terminal workflow.

1. How to create command aliases?

Aliases are custom shortcuts for longer commands. They are perfect for reducing typos and saving keystrokes on commands you run frequently. The syntax is alias name='command'.

Here are some useful aliases you can add to your .bashrc file:

# --- My Custom Aliases ---

# Human-readable ls with all files and sizes
alias ll='ls -lha'

# A more visual and helpful grep
alias grep='grep --color=auto'

# Shortcut to clear the terminal
alias c='clear'

# Constantly updating and upgrading your system? (For Debian/Ubuntu)
alias update='sudo apt update && sudo apt upgrade -y'

# Get your public IP address
alias myip='curl ifconfig.me; echo'

After adding, save and exit the file. After running source ~/.bashrc, you can just type ll instead of ls -lha.

2. How to write powerful shell functions?

While aliases are good for simple command substitutions, they fall short for more complex tasks. This is where shell functions become essential. Functions are ideal when you need to pass arguments to your custom command.

Example 1: How to create and enter a directory (mkcd)?

This is a classic time-saver. Instead of running mkdir directory_name and then cd directory_name, this function does both in one step.

# --- My Custom Functions ---

# Creates a directory and immediately enters it
mkcd ()
{
    mkdir -p -- "$1" && cd -P -- "$1"
}
  • mkdir -p -- "$1": Creates the directory. $1 represents the first argument you pass to the function (the directory name). The -p flag ensures it creates parent directories if needed.
  • &&: This is a logical AND. The cd command will only run if the mkdir command was successful.
  • cd -P -- "$1": Changes into the newly created directory.

For example:

# This single command creates the 'new-project' directory and navigates into it
mkcd new-project

Example 2: How to extract any archive (extract)?

The command-line syntax required to decompress various archive formats, such as .zip, .tar.gz, or .tar.bz2, differs significantly between tools. Instead of having to remember the syntax for all the different tools, you can simplify it into a single command named extract. The function inspects the filename passed as an argument, and using conditional logic, executes the correct underlying decompression or extraction program with the appropriate flags.

# Universal extract function
extract ()
{
    if [ -f "$1" ] ; then
        case "$1" in
            *.tar.bz2)   tar xvjf "$1"    ;;
            *.tar.gz)    tar xvzf "$1"    ;;
            *.bz2)       bunzip2 "$1"     ;;
            *.rar)       unrar x "$1"     ;;
            *.gz)        gunzip "$1"      ;;
            *.tar)       tar xvf "$1"     ;;
            *.tbz2)      tar xvjf "$1"    ;;
            *.tgz)       tar xvzf "$1"    ;;
            *.zip)       unzip "$1"       ;;
            *.Z)         uncompress "$1"  ;;
            *)           echo "'$1' cannot be extracted via extract()" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}

For example:

extract my_files.zip
extract my_other_files.tar.gz

3. How to customize your Bash prompt (PS1)?

You can also customize your terminal by editing the .bashrc file. Your prompt is defined by a special variable called PS1. You can customize it to show colors and useful information, making your terminal much more readable.

Here’s a practical, colored PS1 setting that shows your username, hostname, current directory, and Git branch (if you’re in a Git repository).

# --- Custom Prompt (PS1) ---

# Function to parse git branch
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# The prompt settings
export PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0;31m\]\$(parse_git_branch)\[\033[00m\]\$ "

This looks complex, but it’s just combining colors and special Bash characters:

  • \u: Your username
  • \h: The hostname
  • \w: The full path to the current directory
  • \[\033[...m\]: These are color codes.
  • \$(parse_git_branch): This calls our function to get the current Git branch.

After running source ~/.bashrc, your prompt will transform from user@host:~$ into a colorful, informative line.

4. How to better shell history control?

You can also control how many commands your shell remembers.

  • HISTSIZE: The number of commands kept in memory during a session.
  • HISTFILESIZE: The number of commands saved to the history file (~/.bash_history) when you exit.
# --- History Control ---
export HISTSIZE=10000
export HISTFILESIZE=10000

# Ignore duplicate commands in history
export HISTCONTROL=ignoredups

To explore Bash history commands in more detail, refer to our article on How To Use Bash History Commands and Expansions on a Linux VPS.

5. How to set environment variables and the $PATH?

You can use .bashrc to set environment variables, like your preferred text editor. More importantly, you can add new directories to your $PATH, which is the list of places your shell looks for runnable commands.

For example, if you have a folder of custom scripts in ~/bin:

# --- Environment Variables ---
export EDITOR='nano'  # Set nano as the default text editor

# Add a custom scripts directory to your PATH
export PATH="$HOME/bin:$PATH"

Important: Always add the new path to the beginning of the $PATH variable, followed by a colon, to ensure your custom scripts are found first. For a deeper dive, see this tutorial on how to view and update the Linux PATH environment variable.

Best practices for a clean .bashrc file

Following these bashrc file best practices will save you from future headaches.

  • Always create a backup first: Before making any major changes, run cp ~/.bashrc ~/.bashrc.bak to create a backup.
  • Use comments: Use the # symbol to leave notes explaining what your code does.
  • Keep it organized: Group your configurations into sections (e.g., # Aliases, # Functions).
  • Test changes safely: Before sourcing the file in your current session, you can test the new configuration by opening a new Bash terminal, which will load the new file. If it breaks, just exit to return to your old, working shell.
  • Use version control: For complex setups, consider tracking your .bashrc (and other dotfiles) with Git to manage changes.

Common mistakes to avoid?

  • Forgetting to source: Edits don’t apply until you run source ~/.bashrc or open a new terminal. This is the most common oversight.
  • Wiping the $PATH: Never use export PATH="$HOME/bin". Always include the existing path with export PATH="$HOME/bin:$PATH". Forgetting $PATH will break most of your terminal commands.
  • Syntax errors: A missing quote (') or bracket (}) can break the entire script. If your terminal stops working after an edit, restore your backup.
  • Using aliases for complex logic: If your “alias” needs to accept arguments or has multiple steps, use a function instead of an alias.

Frequently Asked Questions (FAQs)?

1. What does the .bashrc file do in Linux?

The .bashrc file is a user-specific configuration script that runs every time you open a new interactive terminal. It’s used to set up a personalized environment by defining command aliases, shell functions, custom prompts, and environment variables.

2. Where is the .bashrc file located in Linux?

The .bashrc file is located in your user’s home directory. The full path is typically /home/your_username/.bashrc, which can be accessed with the shortcut ~/.bashrc.

3. How do I apply changes after editing .bashrc?

To apply your changes in the current terminal session, you must run the command source ~/.bashrc. Alternatively, you can close the terminal and open a new one, which will automatically load the updated file.

4. What can I add to my .bashrc file?

You can add a wide range of configurations, including:

  • Aliases: Shortcuts for longer commands (alias ll='ls -lha').
  • Functions: More complex custom commands that can take arguments.
  • Environment Variables: Using the export command to set variables like PATH or EDITOR.
  • PS1 Customization: To change the appearance and information in your command prompt.
  • Commands to run on terminal startup.

5. What is the difference between .bashrc and .bash_profile?

.bashrc runs for interactive non-login shells (every new terminal window), making it ideal for aliases and prompt settings. .bash_profile runs for login shells (e.g., an SSH session) and is meant for things that only need to be set once per session, like environment variables. Most systems, however, include logic in .bash_profile to explicitly source .bashrc.

6. How do I restore my .bashrc if my terminal is broken?

If you made a backup with cp ~/.bashrc ~/.bashrc.bak, you can log in through a graphical interface, open a file manager, show hidden files, and manually replace the broken .bashrc with your backup. If you only have command-line access, you might need to use a different shell or recovery mode to run cp ~/.bashrc.bak ~/.bashrc.

Conclusion

In this article, we explored the .bashrc file from its basic function to its role as a powerful customization tool. We covered the essential steps for safely finding and editing the file, as well as applying changes correctly. You have learned how to create practical command aliases, write versatile shell functions, customize your prompt, and manage important environment variables.

By putting these techniques into practice, you can now build a command-line environment that is truly your own. Mastering your .bashrc file is a crucial step in optimizing your Linux terminal and making it a more efficient and productive workspace.

While the functions you’ve learned to write in your .bashrc are perfect for personalizing your interactive shell, more complex automation belongs in standalone script files. Take the next step in your command-line journey by learning how to write and execute shell scripts with our step-by-step guide.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Jayant Verma
Jayant Verma
Author
Manikandan Kurup
Manikandan Kurup
Editor
Senior Technical Content Engineer I
See author profile

With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.

Category:

Still looking for an answer?

Was this helpful?

Thank you. Exactly what I was hoping to find :-)

- dv1937

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.