I found the .bashrc file and I want to know the purpose/function of it. Also how and when is it used?

  • 9
  • You run it as $ bash myFile.bashrc. I found this to be missing :)
    – Pe Dro
    Commented Mar 27, 2020 at 5:26
  • 1
    @PeDro that will start a new shell, initialize it with the settings in that bashrc, and then immediately quit that new shell and return to your old one. It will help you detect errors, but you won't actually get those settings applied. For that you should use source ~/.bashrc: that will load the settings in your current shell.
    – hraban
    Commented Jul 6, 2022 at 18:26

3 Answers 3


.bashrc is a Bash shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt.

You put commands here to set up the shell for use in your particular environment, or to customize things to your preferences. A common thing to put in .bashrc are aliases that you want to always be available.

.bashrc runs on every interactive shell launch. If you say:

$ bash ; bash ; bash

and then hit Ctrl-D three times, .bashrc will run three times. But if you say this instead:

$ bash -c exit ; bash -c exit ; bash -c exit

then .bashrc won't run at all, since -c makes the Bash call non-interactive. The same is true when you run a shell script from a file.

Contrast .bash_profile and .profile which are only run at the start of a new login shell. (bash -l) You choose whether a command goes in .bashrc vs .bash_profile depending on whether you want it to run once or for every interactive shell start.

As a counterexample to aliases, which I prefer to put in .bashrc, you want to do PATH adjustments in .bash_profile instead, since these changes are typically not idempotent:

export PATH="$PATH:/some/addition"

If you put that in .bashrc instead, every time you launched an interactive sub-shell, :/some/addition would get tacked onto the end of the PATH again, creating extra work for the shell when you mistype a command.

You get a new interactive Bash shell whenever you shell out of vi with :sh, for example.

  • 13
    Minor quibble: unlike most other shells, bash does not automatically load the per-instance config file .bashrc when it's started as a login shell. This can sometimes lead to unexpected behavior. The usual workaround is to source .bashrc from .profile or .bash_profile instead. Commented May 13, 2014 at 11:14
  • 3
    @IlmariKaronen Since .bashrc isn't intended for use by other shells, it's better not to source it from .profile (which might be used by other non-bash shells).
    – chepner
    Commented May 14, 2014 at 2:32
  • @chepner That's why most distros do a check in .profile by default where they check if the shell has BASH_VERSION before sourcing .bashrc.
    – oligofren
    Commented Sep 23, 2020 at 13:05
  • @oligofren I don't consider that a solution to rely on, since the default is assuming I am using bash and not some other shell that might be using .profile.
    – chepner
    Commented Sep 23, 2020 at 13:08
  • 1
    @Anil: The kind you get on logging in, as distinct from the kind you get when running a shell script. The term is right there in the manual. Commented Nov 10, 2022 at 16:09

The purpose of a .bashrc file is to provide a place where you can set up variables, functions and aliases, define your (PS1) prompt and define other settings that you want to use every time you open a new terminal window.

It works by being run each time you open up a new terminal, window or pane.

A super minimal one might have the following:

export CLICOLOR=1
export LANG="en_US.UTF-8"
alias cp="cp -i"
alias ls="ls --color=auto"
export PS1="\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
export EDITOR="vim"

That's all you really know to get started

Here's the "overkill" version, useful for experienced developers:

An experienced developer will often have a lot more.
You can see mine here (pic with syntax highlighting):

HISTCONTROL=ignoreboth:erasedups HISTSIZE=100000 HISTFILESIZE=200000
ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -G'
md () { [ $# = 1 ] && mkdir -p "$@" && cd "$@" || echo "Error - no directory passed!"; }
git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; }
HOST='\033[02;36m\]\h'; HOST=' '$HOST
TIME='\033[01;31m\]\t \033[01;32m\]'
LOCATION=' \033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"`'
BRANCH=' \033[00;33m\]$(git_branch)\[\033[00m\]\n\$ '
set -o vi # vi at command line
export EDITOR=vim
test -f ~/.bash_aliases && . $_
test -f ~/.git-completion.bash && . $_
test -s ~/.autojump/etc/profile.d/autojump && . $_
[ ${BASH_VERSINFO[0]} -ge 4 ] && shopt -s autocd
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion
[ -z $TMUX ] && export TERM=xterm-256color && exec tmux
export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$home/.rvm/scripts/rvm"


-1. Set up my history file to ignore duplicates and be much larger than the default.
-2. Color option for ls depending on if you are using linux or OSX
-3. Function "md" to make and cd into a directory with one command
-4. Find the current git branch if in a git repo and...
-5. -9. Define an awesome PS1 prompt, as in <code>20:00:43 durrantm Castle2012 /hom/durrantm/Dropnot/_/rails_apps/linker master \n $ _</code>
-10. Improved PS2 prompt
-11. Set vi as the editor at the command line
-12. Set vi as the default editor
-13. execute my .bash_aliases file if it exists
-14. Execute my git tab completion script (for remotes and branches) if it exists.
-15. Execute autojump if it exists
-16. Allow cd'ing without typing the cd part if the bash version >= 4
-17. Execute a bash completion script if it exists
-18. Use TMUX if it is present
-19. Add rvm to my PATH
-20. Use rvm if it exists.

I've made this portable so that it works on any of my linux or OSX machines without customization - hence a number of tests for presence are done before using certain functions and other scripts.

This also makes it easier to use the entire file immediately on a new machine without having issues that affect opening a new terminal window.

REMINDER - if you change your .bashrc, be sure to start a new terminal window to make sure it works BEFORE rebooting (best practice - do it immediately after the change). If you ever mess up your .bashrc and reboot YOU MAY NOT BE ABLE TO LOGIN! Personally I also create a second 'admin' user on personal machines so that, in such an emergency (broken login), I can log into that and use then use sudo to fix my other login .bashrc file.


It is a bash config file.

Interactive (non-login) shells, then the config is read from these files:

  • $HOME/.bashrc

For Login shells, the config is read from these files:

  • /etc/profile (Always sourced)
  • $HOME/.bash_profile (the rest of these files are checked in order until one is found, then no others are read)
  • $HOME/.bash_login
  • $HOME/.profile

Simple illustration of how/when they are loaded is in the image below.

I added an echo to my .bashrc and .bash_profile

Interactive shell vs login shell

see man bash for more information


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .