0

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.

1

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
    
  • Ah, your last one is right. [ -z "$PS1" ] && return is in my .bashrc! Foolish. – Startec Dec 2 '16 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.