177

The code in .bashrc does not execute when I open a new terminal window in Ubuntu 12.04. I noticed this when creating a .bash_aliases file. The aliases did not show up when I opened a new terminal. However when I type source .bashrc the aliases did show up.

.bashrc should be run every time I open a new terminal window, right?

How do I make this happen?

7
  • 2
    Try .profile instead.
    – jippie
    Jul 8, 2012 at 18:22
  • 5
    @jippie No, .profile is read at login time, not when you open a new terminal, unless there's a misconfiguration somewhere. Selah: did you change anything from the default configuration? Please run ps -o command $$ $PPID in a terminal and copy-paste the output. Jul 8, 2012 at 18:50
  • 1
    Just to make positive sure, you are putting .bashrc in your home folder, right?
    – Jjed
    Aug 5, 2012 at 8:08
  • 4
    Might want to check echo $SHELL. This happened to me and the shell was /bin/sh instead of bash. Fix it in /etc/passwd and its running .bashrc.
    – Joe Flynn
    Jan 28, 2016 at 15:54
  • 4
    In my case deleting unused but existing $HOME/.bash_profile worked.
    – K. Stopa
    Dec 1, 2021 at 13:42

7 Answers 7

248

It isn't necessarily run; at the top of the standard .bashrc is this comment:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

I believe there is an option to run bash terminal as a login shell or not. With Ubuntu, gnome-terminal does not normally run as a login shell, so .bashrc should be run directly.

For login shells (like the virtual terminals), normally the file ~/.profile is run, unless you have either ~/.bash_profile or ~/.bash_login, but they are not there by default. By default, Ubuntu uses only .profile.

The standard ~/.profile has this in it:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

This runs .bashrc if it is available - assuming $BASH_VERSION is present in your environment. You can check for this by entering the command echo $BASH_VERSION, and it should display some information on version number - it should not be blank.

9
  • 4
    After understanding these instructions, I went in my terminal application I went to edit -> profile preferences -> Title and Command -> Run command as a login shell. I unchecked this. Thanks for the help.
    – Selah
    Oct 8, 2012 at 16:36
  • Can you clarify when .profile (hence .bashrc) is loaded/read? It seems this is done when the users logs in to their desktop session. The answer suggests this occurs when you start a new terminal/shell session "For login shells (like the virtual terminals), normally the file ~/.profile is run... "
    – hedgehog
    Jan 7, 2013 at 4:48
  • 5
    It is normally run when you start a new login shell (not really desktop session, because you can run a new login shell from the desktop session). As I said, it is run by default, but not if you have created ~/.bash_profile or ~/.bash_login. You can test by checking or unchecking the checkbox in Edit -> Profile Preferences -> Title and Command -> "Run command as a login shell, and exit then rerun the terminal. You could echo something from .profile to test. Jan 7, 2013 at 21:23
  • 2
    So if you have created ~/.bash_profile or ~/.bash_login on your system and want to keep them, but still want ~/.profile and from there ~/.bashrc executed, you can source it from your own ~/.bash_profile or ~/.bash_login with a line containing source "$HOME/.profile". Gives you back the colored output of ls also in login shells!
    – tanius
    Mar 2, 2014 at 11:40
  • 1
    Thank you for this post... just an FYI, this worked on RHEL also... Oct 12, 2021 at 16:04
76

In my case, simply the .bashrc loader lines were missing in .bash_profile

# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
fi

I added it manually and it worked with my fresh login

1
  • 7
    .bash_profile? Ubuntu uses a .profile by default, not .bash_profile.
    – muru
    May 6, 2016 at 8:30
17

.bash_profile holds configuration for the bash shell. When you open a terminal, it first reads and executes commands from ~/.bash_profile. So you can add the following in .bash_profile to setup the shell according to bashrc.

. ~/.bashrc
1
  • Unless your terminal is running a login shell (which it doesn't by default in Ubuntu), ~/.bash_profile won't be read (and it doesn't exist by default in Ubuntu)
    – muru
    Jan 10, 2017 at 5:49
10

According to the comment in .profile

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.

So there you go; if ~/.bash_profile or ~/.bash_login exists, those will get run instead of ~/.profile.

If you want to run ~/.bashrc just add the line source ~/.bashrc in bash_profile.

1
  • 1
    I have experienced the exact same issue. My .bashrc was being sourced until I created a .bash_profile file. So I prepended source ~/.bashrc line to my .bash_profile file.
    – Lashae
    Aug 29, 2019 at 6:22
7

If $BASH_VERSION is not set, try using the chsh command to set your shell to /bin/bash.

I had a similar issue with 12.04 LTS, and it turned out the new user account had the default shell set to /bin/sh, which was the cause of the problem.

2
  • This was the reason!
    – mulya
    Sep 24, 2015 at 19:21
  • 9 years later, and still ubuntu creates new users with a broken shell... :-/
    – yms
    Feb 5, 2022 at 14:46
4

It was pretty simple for me. I installed mssqltools and somehow it created a file bash_profile in $HOME directory. As bash_profile runs before bashrc is picked up, if you don't have a source command in bash_profile, bashrc won't run.

There are two ways to tackle this, either delete bash_profile or if you use bash_profile, just add the following line anywhere in the file

source ~/.bashrc
1
  • 1
    In my case, meteor installed a .bash_profile. adding source ~/.bashrc worked!
    – dansch
    Aug 29, 2022 at 19:56
2

On Ubuntu 20.x in order for the .bashrc to be read when a terminal is opened, you should copy the template .profile into the user directory (.bashrc is loaded from .profile):

sudo cp /etc/skel/.profile ~/.profile && sudo chown $(whoami):$(whoami) ~/.profile

You must log in to answer this question.

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