35

bash won't source .bashrc from an interactive terminal unless I manually run bash from a terminal:

$ bash

or manually source it:

$ source ./.bashrc

or running:

$ st -e bash

Here's some useful output I hope:

$ echo $TERM
st-256color

$ echo $SHELL
/bin/sh

$ readlink /bin/sh
bash

$ shopt login_shell
login_shell     off

I'm on CRUX Linux 3.0 and I use dwm and st. I've tried using .bash_profile and .profile with no success.

Any ideas?

haste
  • 485
  • For those who wonder st is a not so well known terminal. Mentioning a better known one would have made the question clearer, but I guess one is allowed to insert some little propaganda in a question. – Johan Boulé Jun 06 '24 at 19:46

2 Answers2

58

In .bash_profile make sure you have the following:

# .bash_profile

# If .bash_profile exists, bash doesn't read .profile
if [[ -f ~/.profile ]]; then
  . ~/.profile
fi

# If the shell is interactive and .bashrc exists, get the aliases and functions
if [[ $- == *i* && -f ~/.bashrc ]]; then
    . ~/.bashrc
fi
Jeight
  • 2,699
24

Why would it source it? Your default shell is not bash, but sh:

$ echo $SHELL
/bin/sh

In most modern systems, sh is a symlink to a basic shell. On my Debian for example:

$ ls -l /bin/sh 
lrwxrwxrwx 1 root root 4 Aug  1  2012 /bin/sh -> dash

In your case, sh is a link to bash but, as explained in man bash:

If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. [...] When invoked as an interactive shell with the name sh, bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect.

and

--norc
Do not read and execute the system wide initialization file /etc/bash.bashrc and the personal initialization file ~/.bashrc if the shell is interactive. This option is on by default if the shell is invoked as sh.

So, since your default shell is sh, .bashrc is not read. Just set your default shell to bash using chsh -s /bin/bash.

terdon
  • 254,405
  • 1
    Thank you. I assumed it didn't matter since /bin/sh pointed to /bin/bash. I suppose this issue stemmed from not explicitly assigning the shell to /bin/bash when I originally created the user account. – haste Oct 10 '13 at 18:56
  • Also, Debian/Ubuntu switched to dash for /bin/sh as part of the effort to reduce startup time. This was hilarious when my users started asking why their shell acted differently. – kurtm Oct 10 '13 at 20:14