When I run my program interactively, it works fine:

ssh somehost
$ ~/some/path/somescript.py

When I run my program over ssh directly, it doesn't work. The variable PYTHONPATH is not set, because .bashrc is not sourced.

ssh somehost ~/some/path/somescript.py

If I run ssh somehost 'source ~/.bashrc; ~/some/path/somescript.py', it works fine.

But the latter wouldn't work for some other folks, e.g. using tcsh and not having a ~/.bashrc at all).

What is the command to run a thing via ssh on another host that works for all shells?

  • 1
    Answered here, stackoverflow.com/questions/820517/bashrc-at-ssh-login, basically, ssh doesn't source .bashrc, you need to source your .bashrc via .bash_profile or other options. – EightBitTony Jun 17 '11 at 22:49
  • 1
    @EightBitTony: No, that's a different issue. bzdjamboo's question is about a non-interactive session. – Gilles 'SO- stop being evil' Jun 17 '11 at 23:16
  • Actually the last answer in the link I posted appears to be valuable, ssh does source .bashrc, but it doesn't execute due to the check for being interactive. I appreciate however, that your answer covers all the bases. – EightBitTony Jun 17 '11 at 23:27
  • 3
    @EightBitTony: On that question, the accepted answer is a good solution for the asker but the answers don't tell the full story. SSH itself doesn't source anything. If your login shell is bash invoked as bash, then it sources .bash_profile or .profile for an interactive login, and .bashrc for a non-interactive login. If your login shell is bash invoked as sh or ash or ksh, it sources .profile for an interactive login and nothing for a non-interactive login. – Gilles 'SO- stop being evil' Jun 18 '11 at 7:45

There is no standard per-user file that is run for non-interactive logins. You need to either make the program self-contained, so that it's able to find its dependencies without relying on non-default environment variables, or else explicitly set the environment, typically with

ssh somehost '. ~/.profile; exec ~/some/path/somescript.py'

You shouldn't be setting environment variables in .bashrc: this file is meant for interactive shells, and is read in each instance of bash. Environment variables should be set in .profile, which is read when you log in. Apart from the very small number of people who don't use a Bourne-style shell as their login shell, .profile does work for everyone, whether they use bash or zsh or csh or fish interactively. See also Difference between .bashrc and .bash_profile, Which setup files should be used for setting up environment variables with bash?.

That being said, there is a way to source a script every time you log in over ssh with a particular key. See Is there a way to push shell config information when SSHing to a host?, sh startup files over ssh.

|improve this answer|||||
  • Now how do I mark it "answered"? :-) – bzdjamboo Jun 18 '11 at 4:26
  • Along the left side of this answer section there should be up and down vote arrows and a vote counter. Just below that there should be the outline of a checkbox. You can click that to activate the answer accept function. – Caleb Jun 18 '11 at 6:16
  • Thankl you all again, but apparently your answers didn't solve my problem. – bzdjamboo Jun 20 '11 at 18:33
  • Here is a bit newer formulation of the problem and my current understanding of the root of it. – bzdjamboo Jun 20 '11 at 18:35
  • Seems that I cannot add more than 60 or so chars here so I've got to ask my question separately again. Sorry ab. that. Again, this one is not solved, and digging much more for a couple of days didn't help. – bzdjamboo Jun 20 '11 at 18:46

I had the same problem, but for me the solution was different. My user was not configured to use bash as shell, it used zsh as shell instead, therefore the bash dot files were not run at login. Open /etc/passwd with a text editor and look for your username and what shell it uses:


This is how my user entry looks. Notice it says /bin/zsh instead of /bin/bash. For zsh, the correct dot file is ~/.zprofile. Its contents will be run every time you login using ssh.

|improve this answer|||||

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.