Hacker News new | ask | show | jobs
by eslaught 4719 days ago
> As the name implies, .bashrc is for bash configs. Environment variables or other configuration settings should typically be written to .profile on Ubuntu and .bash_profile on OS X.

I don't understand this. If I set PATH in .profile or .bash_profile, then on Ubuntu I won't see that setting when I log in via a non-login shell. This is common for non-interactive SSH sessions.

So consider the following example:

  * I install MPI to /usr/local/openmpi-X.Y.Z
  * I add /usr/local/openmpi-X.Y.Z/bin to PATH, inside my .profile
Now MPI is broken, because MPI executes out of a non-login shell when running on remote nodes via SSH.

I think I'll keep my PATH settings in .bashrc, thank you very much.

1 comments

~/.profile is executed only at login on Ubuntu so changing your PATH there will be applied the next time you login. You can source the ~/.profile in your current non-login shell if you need the PATH change right now. I've tried to tweak the second paragraph in the "When not to modify .bashrc?" section to make this more clear.

Thanks for bringing this up.

The problem is that running an MPI program on a remote node starts a new non-interactive SSH session. That SSH session has no parent login session, so it won't inherit environment variables from any session that read the .profile file. And because the SSH login is non-interactive, the only file that the shell inside the SSH session reads is .bashrc.

Sure, you could source .profile manually, but this is awkward to code inside an MPI application. In addition, this will cause .bashrc to get sourced twice, because your shell already read .bashrc when it started the SSH session. Hopefully your .bashrc is idempotent, but even so, it feels wrong to have to read .bashrc twice.

Try this on a remote server:

    remote:~$ cat .profile
    echo "read .profile"

    echo "sourcing .bashrc from .profile"
    source ~/.bashrc
    remote:~$ cat .bashrc
    echo "read .bashrc"
And then from your local machine:

    local:~$ ssh remote
    read .profile
    sourcing .bashrc from .profile
    read .bashrc
    local:~$ ssh remote -k true
    read .bashrc