2

in my .profile file I have the line:

# source bashrc if it exists
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi

to make sure that my .bashrc file is sourced if it exists. I also have a set up script that I run on new machines to set up my environment that is for some reason behaving differently.

At the end of the setup script I have:

# read .profile
source "$USERDIR/.profile"

Where $USERDIR points to my home directory. I know that .profile is properly sourced when this script executes because I echo ".profile sourced", and this is printed when the script finishes.

However, the .bashrc is not read from .profile (when .profile is sourced from this setup file). I can't figure out why because just typing source .profile from bash works as it should.

I know .bashrc is not run because the echo line in my rc file echoes only when I source .profile directly and not from this set up script. The .bashrc script does exist when I try to source it (so the if [ -f ~/.bashrc ] should execute) because it is created at the same time .profile is.

2 Answers 2

2

One step at a time. Add this to .profile

# source bashrc if it exists
if [ -f ~/.bashrc ]; then
    echo ".profile is sourcing " ~/.bashrc
    source ~/.bashrc
fi

and then call bash as login: bash -l should be enough.

Does the line: .profile is sourcing /home/user/.bashrc get printed?

If it does, the problem may be with:

  • do any of this files exist: ~/.bash_profile, ~/.bash_login
  • is bash being called with the option --noprofile?
  • or with your setup script.

If it doesn't, the problem is with .profile and or .bashrc.

  • maybe you should use $HOME instead of ~ for the user directory.
  • maybe your bashrc has a line to prevent execution when not interactive:

    # If not running interactively, don't do anything
    [ -z "$PS1" ] && return
    
1
  • Ah, your last one is right. [ -z "$PS1" ] && return is in my .bashrc! Foolish.
    – Startec
    Commented Dec 2, 2016 at 2:41
0

Where $USERDIR points to my home directory.

And where exactly does $HOME point? In bash, ~ always expands to wherever $HOME points. If you can't source ~/.profile because $HOME is not set up correctly, then your $USERDIR/.profile won't be able to source ~/.bashrc.

You must log in to answer this question.

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