.bashrc

Also known as: bashrc

On this page 7

.bashrc is a per-user startup script in the home directory that the Bash shell executes every time it starts an interactive non-login shell, such as a new terminal window on a Linux desktop. It holds the settings that shape daily command-line work: aliases, shell functions, the prompt, shell options and environment variables.

The name follows the Unix convention of rc files, short for run commands, and the leading dot makes the file hidden in ordinary directory listings. In the shell and terminal toolset of Linux, .bashrc is the first place administrators and developers look when a shell behaves differently from one account to another.

When Bash reads .bashrc

Bash decides which startup files to read based on two properties of the shell: whether it is a login shell and whether it is interactive. An interactive non-login shell reads ~/.bashrc directly. A login shell, such as an SSH session or a text console login, reads /etc/profile and then the first file found among ~/.bash_profile, ~/.bash_login and ~/.profile, and it does not read .bashrc on its own.

Because of this split, most distributions ship a default ~/.bash_profile or ~/.profile that sources .bashrc explicitly, so that login and non-login sessions share the same interactive settings. Non-interactive shells that run scripts read neither file, unless the BASH_ENV variable points to a file.

The table below shows which files each type of shell reads at startup.

Shell typeTypical exampleFiles read
Interactive loginSSH session, console login/etc/profile, then first of the profile files
Interactive non-loginNew terminal tab on a desktopSystem bashrc, then ~/.bashrc
Non-interactiveRunning a shell scriptFile named in BASH_ENV, if set
Invoked as shScript with a POSIX shebang/etc/profile and ~/.profile in login mode
Startup files read by each type of Bash shell

Many systems also have a system-wide file, /etc/bash.bashrc on Debian and Ubuntu or /etc/bashrc on Fedora and related distributions. The exact loading path differs by distribution, so the default skeleton files in /etc/skel are the reliable reference for a given system.

What belongs in .bashrc

.bashrc is meant for settings that only matter in an interactive session. Typical contents include shell aliases such as alias ll='ls -alF', small shell functions, the PS1 prompt definition, history settings such as HISTSIZE and HISTCONTROL, shell options set with shopt, and the loading of tab completion scripts.

The following table groups the common contents of .bashrc by purpose.

CategoryExample settingEffect
Aliasesalias gs=’git status’Shortens frequent commands
PromptPS1 with user, host and directoryShows context before each command
HistoryHISTSIZE=10000, histappendKeeps more commands across sessions
Shell optionsshopt -s globstarEnables recursive directory globbing
CompletionSource bash-completion scriptAdds context-aware tab completion
Functionsmkcd to create and enter a directoryPackages repeated steps
Common contents of a .bashrc file

A good default file starts with an interactivity guard such as [[ $- == *i* ]] || return. This line stops the file early when it is sourced by a non-interactive process, which prevents output from breaking tools like scp, rsync or remote command execution over SSH.

How .bashrc differs from other shell startup files

The central rule is that environment belongs in the profile and interactive behavior belongs in .bashrc. Environment variables such as PATH are inherited by child processes, so setting them once at login is enough, while aliases and functions are not inherited and must be redefined in every interactive shell.

The table compares .bashrc with the neighboring files that users most often confuse it with.

FileRead byBest used for
~/.bashrcInteractive non-login BashAliases, prompt, functions, options
~/.bash_profileLogin Bash onlyEnvironment, then sourcing .bashrc
~/.profileLogin shells, several shellsShell-neutral environment variables
~/.bash_logoutLogin Bash on exitClearing the screen or temporary files
~/.zshrcInteractive ZshZsh equivalent of .bashrc
How .bashrc differs from related startup files

On macOS, Terminal historically opened every window as a login shell, so settings placed only in .bashrc seemed to be ignored. Current macOS versions use Zsh by default, which reads ~/.zshrc instead, so a user who switches shells must move or translate the configuration.

Applying and testing changes safely

Edits to .bashrc take effect only in shells started after the change. Running source ~/.bashrc or its short form . ~/.bashrc applies the file to the current session, although it does not remove aliases or variables that were deleted from the file.

Before editing, it is wise to keep a second terminal open or make a backup copy. A syntax error or an exit command placed in the wrong spot can make every new shell close immediately, and a sensible recovery is to start bash --norc and repair the file. Running bash -n ~/.bashrc checks syntax without executing anything.

For larger setups, many users split configuration into separate files, for example ~/.bashrc.d/aliases.sh, and source them in a loop. Keeping these files in a version-controlled dotfiles repository makes the same environment reproducible across servers and workstations.

Common mistakes with .bashrc

Most .bashrc problems come from putting settings in the wrong file or from commands that assume an interactive terminal. The table below lists frequent symptoms with their cause and fix.

SymptomCauseFix
Aliases missing over SSHProfile does not source .bashrcSource .bashrc from .bash_profile
scp or rsync failsFile prints text when non-interactiveAdd the interactivity guard
PATH grows with duplicatesPATH appended in every nested shellSet PATH in the profile instead
Slow terminal startupHeavy commands run at every launchLazy-load tools or cache results
Every new shell exitsSyntax error or stray exit commandRepair from bash –norc
Frequent .bashrc problems and their fixes

A further risk is storing secrets such as API tokens in .bashrc. The file is often copied into public dotfiles repositories, so credentials belong in a separate file with restrictive permissions or in a dedicated secret manager.

In everyday language

The .bashrc file is like a personal checklist that the command line reads each time you open a new terminal window. It tells the shell how you like things set up. It saves your shortcuts, your preferred prompt and your habits, so you do not have to type them again every day.

Each person on a computer has their own copy. Changing it only affects your own sessions, and the changes appear the next time you open a terminal.

Seen in the real world

A site reliability engineer manages about 60 Linux servers and often works in several terminal tabs at once. She keeps a single .bashrc in a dotfiles repository and deploys it to every server with a configuration management tool.

The file sets HISTSIZE to 50,000 with timestamps, so she can reconstruct exactly which commands ran during an incident. It defines a prompt that shows the hostname in red on production machines and in green on staging, which sharply reduces the risk of running a command on the wrong server. An interactivity guard at the top keeps automated rsync backups working, and the same file sources a tab completion script for the cluster management tool her team uses.