By Jayant Verma and mkurup

The .bashrc file is a per-user shell script that the Bash shell reads and runs every time it starts an interactive, non-login session, such as opening a new terminal window. It lives in your home directory at ~/.bashrc (which expands to /home/your_username/.bashrc), and it is the standard place to store the aliases, functions, environment variables, and prompt settings that personalize your command line. If you have ever worked with a Linux terminal, this small script is the key to making it work more efficiently for you.
In this article, you will learn what the .bashrc file is, where to find it, and how to edit it safely. You will see how Bash decides which startup files to read, how .bashrc differs from .bash_profile, .profile, and .zshrc, and how to create time-saving aliases, write shell functions, customize your prompt, and manage your $PATH. You will also get a complete, copy-ready sample .bashrc file and a troubleshooting section for recovering when an edit breaks your shell.
Key Takeaways:
.bashrc file is a personal shell script that automatically configures your environment every time you open a new interactive, non-login terminal..bashrc is read by interactive non-login shells, while .bash_profile (or .profile) is read once at login, which is the core distinction between the two files.source ~/.bashrc from ~/.bash_profile so your settings load in both login and non-login sessions.cp ~/.bashrc ~/.bashrc.bak before editing, because a single syntax error can stop your shell from starting.source ~/.bashrc (or its . ~/.bashrc shortcut) to apply changes to the current session without opening a new terminal.$PATH, always include the existing value (export PATH="$HOME/bin:$PATH") so you do not wipe out the system path.~/.zshrc instead of ~/.bashrc; if you switch shells, you migrate your aliases, functions, and exports rather than reusing the same file..bashrc locks you out, start a clean shell with bash --norc (or bash --noprofile --norc) to fix the file safely..bashrc file?The .bashrc file is a shell script that the Bash shell runs whenever it starts interactively. In simple terms, every time you open a new terminal window, Bash reads and executes the commands inside this file. That makes it the ideal place for your personal Linux environment configuration.
It lets you store and automatically apply:
It is a hidden file located in your user’s home directory (~/), which is why a plain ls command will not display it. The leading dot in the filename is the long-standing Unix convention for “hidden” configuration files, and it is why per-user shell settings have lived in dotfiles like .bashrc since the early days of Unix.
.bashrc located?The .bashrc file is located in your user’s home directory. The full path is typically /home/your_username/.bashrc, which you can reach with the ~/.bashrc shortcut. Because the filename begins with a dot, it is hidden from a normal directory listing, so you need a flag to see it.
To list all files in your home directory, including hidden ones, use ls -a.
- ls -a
The output includes the dotfiles in your home directory, with .bashrc among them:
Output. .bash_history .bashrc .config .local .ssh
.. .bash_logout .cache .gnupg .profile
In some minimal or server installations, a .bashrc file might not exist yet. If you run ls -a and do not see it, you can create it with the touch command.
- touch ~/.bashrc
Once the file exists, you can open it and start adding your configurations.
.bashrc run?The .bashrc file runs whenever Bash starts an interactive shell that is not a login shell, which is the most common case on a desktop: opening a new terminal window or tab. According to the official GNU Bash Reference Manual, an interactive non-login shell “reads and executes commands from ~/.bashrc, if that file exists.”
Note: A standard SSH session (ssh user@host) opens an interactive login shell, which reads .bash_profile or .profile and not .bashrc directly. Your .bashrc settings still apply in SSH sessions because most distributions ship a .bash_profile (or .profile) that sources ~/.bashrc automatically. The GNU Bash Manual also notes that Bash reads ~/.bashrc when it detects a non-interactive network connection via the legacy remote shell daemon (rshd), but this is a rare historical scenario and not the standard SSH workflow.
What .bashrc does not do is run automatically for login shells (which read .bash_profile or .profile instead) or for non-interactive script execution. The next section explains exactly how Bash chooses between these files.
When you start a shell session, it does not just look for .bashrc at random. Bash follows a specific order to decide which configuration files to load, and that order depends on whether the shell is a login or non-login shell and whether it is interactive or non-interactive.
The distinction between login and non-login shells determines which files Bash reads, so it is worth understanding before you edit anything.
/etc/profile, then looks for ~/.bash_profile, ~/.bash_login, and ~/.profile in that order, and runs only the first one it finds.~/.bashrc. This is the most common scenario for Linux desktop users.macOS note: Terminal.app and iTerm2 on macOS open login shells by default, which means they read .bash_profile instead of .bashrc when you open a terminal window. This is the opposite of most Linux terminal emulators (GNOME Terminal, Konsole, and similar). If you use Bash on macOS, put your customizations in ~/.bash_profile, or source ~/.bashrc from it.
.bashrc nor .bash_profile by default. It instead checks the BASH_ENV variable and sources whatever file that names, if any.When an interactive login shell exits, Bash also reads ~/.bash_logout if it exists, which is a useful place for cleanup commands.
Putting the rules together, the order in which Bash reads files depends on the session type. The numbered chain below traces a typical interactive login session, which is the path that most often confuses people.
/etc/profile runs first, applying system-wide login settings for every user.~/.bash_profile, ~/.bash_login, and ~/.profile.~/.bashrc (shown below), so your interactive settings load too.~/.bashrc directly. Many distributions also have ~/.bashrc source the system-wide file for shared defaults (/etc/bash.bashrc on Debian/Ubuntu, /etc/bashrc on RHEL/Fedora).Most distributions ship a ~/.bash_profile or ~/.profile that explicitly checks for and runs ~/.bashrc, which is what unifies your environment across login and non-login sessions. A typical snippet looks like this:
# Inside ~/.bash_profile: load ~/.bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Without this snippet, the aliases and functions you put in .bashrc would not be available in an SSH login session, which is a frequent source of confusion.
.bashrc vs .bash_profile vs .profile: key differencesA major point of confusion is the .bashrc versus .bash_profile debate, with .profile adding a third option. The following table compares the main configuration files so you can decide where each setting belongs.
| File | Shell type | When it runs | Typical use case |
|---|---|---|---|
/etc/bash.bashrc (Debian/Ubuntu) or /etc/bashrc (RHEL/Fedora) |
System-wide | Every user’s interactive, non-login shell | Default aliases and functions for all users on the system. |
~/.bashrc |
User-specific | A user’s interactive, non-login shells | The main file for personal aliases, functions, and prompt customizations. |
~/.bash_profile |
User-specific | A user’s login shell | Environment variables and commands that should run once per session. |
~/.profile |
User-specific | Fallback for ~/.bash_profile |
A more generic version usable by other shells, not just Bash. |
For day-to-day terminal customizations like aliases and prompt settings, ~/.bashrc is the correct file to edit. For variables and commands that only need to be set once at login, use ~/.bash_profile or ~/.profile.
.bashrcBefore you make any changes, you should create a backup. A simple syntax error in your .bashrc can prevent your terminal from starting correctly, so a backup is your safety net.
The first step is to create that backup with the cp command.
- cp ~/.bashrc ~/.bashrc.bak
If you ever run into trouble, you can restore this backup and get back to a working shell.
nano is a beginner-friendly editor that is installed by default on most distributions. Open the file with the following command.
- nano ~/.bashrc
Make your edits, then press Ctrl+O followed by Enter to save, and Ctrl+X to exit. Because nano shows its keyboard shortcuts at the bottom of the screen, it is a good choice if you are new to editing files from the command line.
If you prefer vim (or the lighter vi), open the file the same way.
- vim ~/.bashrc
Press i to enter insert mode, make your changes, then press Esc, type :wq, and press Enter to write the file and quit. Use whichever editor you are comfortable with; the file content is identical regardless of how you edit it.
Once you save your edits, they will not take effect immediately, which is covered in the section on reloading .bashrc further below.
.bashrcAliases are custom shortcuts for longer commands. They are perfect for reducing typos and saving keystrokes on commands you run frequently.
The syntax for an alias is alias name='command', with no spaces around the = sign. The part before the equals sign is the new shortcut you type, and the quoted part is the command Bash runs in its place. Quoting the command keeps multi-word commands and special characters intact.
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'
# Update and upgrade packages (for Debian and Ubuntu)
alias update='sudo apt update && sudo apt upgrade -y'
# Get your public IP address (pings an external service; replace with your preferred endpoint)
alias myip='curl ifconfig.me; echo'
After adding these lines, save and exit the file, then run source ~/.bashrc to reload it. You can then type ll instead of ls -lha. For more text-search shortcuts, the grep tutorial covers the underlying command in depth.
You can use .bashrc to set environment variables, such as your preferred text editor, and to extend your $PATH, which is the list of directories your shell searches for runnable commands.
The export keyword marks a variable so that programs you launch from the shell inherit it. Without export, the variable exists only in the current shell and is not passed to child processes.
# --- Environment Variables ---
export EDITOR='nano' # Set nano as the default text editor
After sourcing the file, any program that respects the EDITOR variable (such as git opening a commit message) will use nano.
PATH variableTo make a directory of custom scripts runnable from anywhere, add it to $PATH. The key is to include the existing $PATH value so you extend it rather than replace it.
# Add a custom scripts directory to your PATH
export PATH="$HOME/bin:$PATH"
Placing $HOME/bin at the front means your custom scripts are found before system commands of the same name; placing it at the end (export PATH="$PATH:$HOME/bin") gives system commands priority instead. Either way, always include $PATH so you do not wipe out the existing directories. For a deeper dive, see the tutorial on how to view and update the Linux PATH environment variable.
You can also change how your terminal looks by editing the .bashrc file. Your prompt is defined by a special variable called PS1, which you can customize to show colors and useful information, making your terminal more readable.
The following example is a colored PS1 that shows your username, hostname, current directory, and Git branch when you are inside a Git repository.
# --- Custom Prompt (PS1) ---
# Function to parse the current git branch (handles detached HEAD state)
parse_git_branch() {
git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null
}
# 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 just combines colors and special Bash characters:
\u: Your username.\h: The hostname.\w: The full path to the current directory.\[\033[...m\]: Color codes that Bash interprets as non-printing characters.\$(parse_git_branch): A call to the function above that prints the current Git branch (or a short commit hash when in detached HEAD state).After running source ~/.bashrc, your prompt transforms from a plain user@host:~$ into a colorful, informative line.
For more advanced prompt control, Bash also provides the PROMPT_COMMAND variable. When set, Bash runs its value as a command just before printing each prompt. This makes it useful for updating the title bar, syncing history across sessions, or injecting dynamic values that the PS1 escape sequences cannot express directly:
# Run a command before each prompt is displayed
export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
.bashrcWhile aliases are good for simple command substitutions, they fall short for more complex tasks. This is where shell functions become essential, because functions can accept arguments and run multiple commands with conditional logic.
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"
}
Each part of the function plays a specific role:
mkdir -p -- "$1": Creates the directory. $1 is the first argument you pass to the function (the directory name), and the -p flag creates parent directories if needed.&&: A logical AND, so cd only runs if mkdir succeeds.cd -P -- "$1": Changes into the newly created directory.You can then create and enter a directory in one command.
- mkcd new-project
The command-line syntax for decompressing archive formats such as .zip, .tar.gz, or .tar.bz2 differs from tool to tool. Instead of remembering every variant, you can wrap them in a single function named extract. The function inspects the filename you pass and uses a case statement to run the correct extraction program with the right flags.
# Universal extract function
extract ()
{
if [ -f "$1" ] ; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.tar.xz) tar xvJf "$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" ;;
*.xz) unxz "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
With the function in place, you can extract different archive types with the same command.
- extract my_files.zip
- extract my_other_files.tar.gz
You can also control how many commands your shell remembers, which is handy when you reuse long commands.
The two main variables are HISTSIZE and HISTFILESIZE:
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
One more useful setting is shopt -s histappend. By default, Bash overwrites ~/.bash_history when a session ends. Setting histappend makes each session append to the file instead, so history from multiple terminal windows is preserved rather than one window’s history wiping out the others:
# Append to history file instead of overwriting it on shell exit
shopt -s histappend
To explore Bash history in more detail, refer to the tutorial on how to use Bash history commands and expansions on a Linux VPS.
.bashrc without restarting the terminalWhen you edit .bashrc, your changes do not apply to the current session automatically, because the file only runs when a new shell starts. You have two ways to apply changes immediately.
source commandThe source command reads and executes the file in your current shell session, which is the standard way to apply .bashrc changes without interrupting your workflow.
- source ~/.bashrc
After this runs, every alias, function, and variable you added is available in the session you are already in.
Bash also provides a shorter, equivalent command: a single dot followed by the file path. It behaves exactly like source.
- . ~/.bashrc
Both commands re-execute the file in place. Pick whichever you find easier to remember; the result is identical.
.bashrc exampleTo tie everything together, here is a complete, annotated .bashrc you can adapt. It groups settings into clearly labeled sections covering history, aliases, environment variables, the $PATH, the prompt, and functions.
# ~/.bashrc: a practical starter configuration
# --- History Control ---
export HISTSIZE=10000
export HISTFILESIZE=10000
export HISTCONTROL=ignoredups:erasedups # Ignore and dedupe repeated commands
shopt -s histappend # Append to history file; don't overwrite
# --- Aliases ---
alias ll='ls -lha' # Detailed, human-readable listing
alias grep='grep --color=auto' # Highlight matches
alias c='clear' # Clear the screen
alias update='sudo apt update && sudo apt upgrade -y' # Debian/Ubuntu update
# --- Environment Variables ---
export EDITOR='nano' # Default editor
# --- PATH ---
export PATH="$HOME/bin:$PATH" # Prefer scripts in ~/bin
# --- Custom Prompt (PS1) ---
parse_git_branch() {
git symbolic-ref --short HEAD 2>/dev/null || git rev-parse --short HEAD 2>/dev/null
}
export PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0;31m\]\$(parse_git_branch)\[\033[00m\]\$ "
# --- Functions ---
mkcd () {
mkdir -p -- "$1" && cd -P -- "$1" # Make a directory and enter it
}
After saving the file, apply it with source ~/.bashrc. You can remove any section you do not need; the comments make it clear what each block does.
.bashrc vs .zshrc: what changes if you use Zsh?Zsh (the Z shell) has become the default shell on several modern systems, including macOS since the Catalina release and Kali Linux since version 2020.4, as documented in the Kali Linux 2020.4 release notes. If you switch your default shell to Zsh, Bash stops reading .bashrc, and Zsh reads ~/.zshrc instead.
The good news is that the two files serve the same purpose, and most of your configuration transfers with little change:
alias name='command' syntax is identical in both shells.export statements also work the same way, so your $PATH and EDITOR lines carry over unchanged.PROMPT (or PS1) escape sequences, so a complex Bash PS1 often needs adjusting.In practice, switching shells means migrating your aliases, functions, and exports from .bashrc to .zshrc rather than reusing the same file. If you maintain both shells, keep the shared settings (like aliases and exports) in a separate file and source it from each shell’s startup file so you only edit them in one place.
.bashrc fileFollowing these .bashrc best practices will save you from future headaches.
cp ~/.bashrc ~/.bashrc.bak to create a backup.# symbol to leave notes explaining what your code does.# Aliases and # Functions)..bashrc and other dotfiles with Git to manage and roll back changes..bashrc issuesEditing .bashrc occasionally breaks a shell, but every common problem has a clear fix. The subsections below cover the issues you are most likely to hit.
.bashrcA syntax error, such as a missing quote or an unbalanced bracket, can cause Bash to fail when it sources ~/.bashrc, which may make new terminals unusable. To recover, start a shell that skips all startup files so you can edit the broken file from a clean environment.
- bash --noprofile --norc
This skips both ~/.bash_profile and ~/.bashrc, making it the safest universal recovery method regardless of which file contains the error. If you are certain the problem is only in ~/.bashrc and your login shell is unaffected, bash --norc alone is sufficient.
- bash --norc
From that clean shell, open ~/.bashrc, fix the error (or restore your backup with cp ~/.bashrc.bak ~/.bashrc), and source the file again. If you only have command-line access and cannot launch any shell, boot into recovery mode or use a file manager with hidden files visible to replace the file with your backup.
If your new alias or variable does not work, the usual cause is that you edited the file but never reloaded it. The file only runs when a shell starts, so the current session still holds the old configuration. Apply your changes with the following command.
- source ~/.bashrc
If the setting still does not appear, confirm you edited the right file (an interactive non-login shell reads ~/.bashrc, not ~/.bash_profile), and check for a typo in the variable or alias name.
To catch errors before they affect your session, ask Bash to read the file in syntax-check mode, which parses the file without executing it.
- bash -n ~/.bashrc
If the command prints nothing, the syntax is valid. If it reports an error, it tells you the approximate line so you can fix it before sourcing. You can also test more safely by opening a brand-new terminal window: the new shell loads your changes, and if something breaks, your current shell stays untouched.
For deeper analysis beyond basic syntax, shellcheck is the community-standard linter for shell scripts. It catches not just syntax errors but also common logical mistakes, quoting pitfalls, and portability issues that bash -n misses:
- shellcheck ~/.bashrc
shellcheck is available in the package repositories of most distributions (sudo apt install shellcheck on Debian/Ubuntu, sudo dnf install ShellCheck on Fedora/RHEL).
A few recurring mistakes account for most .bashrc trouble, so it is worth knowing them in advance.
source ~/.bashrc or open a new terminal. This is the most common oversight.$PATH: Never write export PATH="$HOME/bin" on its own. Always include the existing path with export PATH="$HOME/bin:$PATH", or you will break most of your terminal commands.') or bracket (}) can break the entire script. If your terminal stops working after an edit, restore your backup..bashrc file do in Linux?The .bashrc file is a user-specific shell script that runs every time you open a new interactive, non-login terminal. It is the standard place for aliases, shell functions, custom prompts, and environment variables. See the What is a .bashrc file? section above for a full explanation.
.bashrc file located in Linux?At ~/.bashrc, which expands to /home/your_username/.bashrc. Because its name starts with a dot, you need ls -a to see it. See Where is .bashrc located? for details.
.bashrc?Run source ~/.bashrc (or . ~/.bashrc) in your current terminal, or close and reopen the terminal. The file only executes when a new shell starts, so edits have no effect until you reload it. See How to reload .bashrc without restarting the terminal for the full walkthrough.
.bashrc file?You can add a wide range of configurations, including:
alias ll='ls -lha'.export command to set variables like PATH or EDITOR..bashrc and .bash_profile?.bashrc runs for interactive non-login shells (every new terminal window), making it ideal for aliases and prompt settings, while .bash_profile runs for login shells (such as an SSH session) and is meant for things that only need to be set once per session, like environment variables. Most systems include logic in .bash_profile to explicitly source .bashrc, which is why your aliases still work after an SSH login.
.bashrc if my terminal is broken?If you made a backup with cp ~/.bashrc ~/.bashrc.bak, you can restore it by running cp ~/.bashrc.bak ~/.bashrc from any working shell. If a broken file prevents Bash from starting, launch a clean shell with bash --norc (or bash --noprofile --norc for login shells), fix or restore the file, then source it again. If you have no command-line access at all, use a graphical file manager with hidden files shown, or boot into recovery mode, to replace the broken file.
.bashrc and .zshrc?Both files serve the same purpose for their respective shells: .bashrc configures Bash and .zshrc configures Zsh. If you switch your default shell to Zsh (now the default on macOS and on distributions like Kali Linux), you migrate your aliases, functions, and exports from .bashrc to .zshrc. Aliases and export statements usually transfer unchanged, but the prompt syntax differs between the two shells.
.bash_profile or .bashrc?Use .bashrc for aliases, functions, and shell behavior that applies to interactive shell sessions, and use .bash_profile (or .profile) for environment variables and PATH modifications that should be set once at login. Many setups source .bashrc from .bash_profile to consolidate configuration so you only maintain one file for interactive settings.
~/.bashrc after editing it?Changes to .bashrc do not take effect in the current shell session automatically, because the file runs only when a new shell starts. Running source ~/.bashrc or . ~/.bashrc re-executes the file in the current session without requiring a new terminal window, so your latest aliases and variables become available immediately.
.bashrc prevent my terminal from opening?Yes. A syntax error in .bashrc can cause Bash to fail on startup, which may make the terminal unusable. To recover, open a shell that skips your startup files with bash --noprofile --norc, then edit and fix the file. You can also boot to a recovery shell or use a text editor outside the terminal, then restore your backup if you have one.
.bashrc read for all types of shells?No. The .bashrc file is read only by interactive non-login Bash shells. It is not read by login shells (which read .bash_profile or .profile) or by non-interactive shells such as scripts. To make a variable available in scripts, export it from the appropriate login file or set BASH_ENV to point at a file.
PATH in .bashrc?Add a line that prepends or appends the directory to the existing $PATH, then reload the file. For example, to add /your/directory, put this in ~/.bashrc:
export PATH="$PATH:/your/directory"
Then run source ~/.bashrc to apply the change immediately. Always include $PATH in the value so you extend the existing path rather than overwriting it.
.bashrc exist by default on all Linux distributions?Most Debian-based distributions (Ubuntu, Debian) create a default .bashrc file when a new user account is created. On minimal or server installations, it may not exist. You can create it manually by running touch ~/.bashrc and then adding configuration as needed.
In this article, you explored the .bashrc file from its basic function to its role as a powerful customization tool. You learned where the file lives, how Bash decides which startup files to read, and how .bashrc differs from .bash_profile, .profile, and .zshrc. You also saw how to safely edit and reload the file, create practical aliases, write reusable shell functions, customize your prompt, manage your $PATH, and recover when an edit breaks your shell.
By putting these techniques into practice, you can build a command-line environment that is truly your own. Mastering your .bashrc file is a key step in making your Linux terminal a more efficient and productive workspace.
While the functions you write in .bashrc are perfect for personalizing your interactive shell, more complex automation belongs in standalone script files. Take the next step by learning how to write and execute shell scripts with the step-by-step guide.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Scale up as you grow — whether you're running one virtual machine or ten thousand.

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.
