186

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?

chicks
  • 574
Selah
  • 2,995

7 Answers7

259

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.

Marty Fried
  • 18,944
83

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

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
muru
  • 207,228
SD.
  • 271
12

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.

Zanna
  • 72,312
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.

evan_b
  • 170
  • 1
  • 4
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
Zanna
  • 72,312
redzack
  • 145
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