Possible Duplicate:
dot file not sourced when running a command via ssh

This is a continuation of prev. my question dot file not sourced when running a command via ssh (I could not add enough information at the orig. post as it only allows 600 characters. Sorry if I did it wrong).

In a nut shell: if I have bash as the default shell on host1 and on host2, and

if I run from host1 the following:

ssh host2 ~/some/path/somescript.py it doesn't work.

This is because none of the .-files are sourced on the remote host so my PYTHONPATH is not set and =the command fails.

This brings us to a shorter version of the problem:

There is no . file that is sourced when I run bash on a remote host (i.e. non-interactively).

To prove this, I created on the remote host the following files:

  • .bash_login
  • .bash_profile
  • .bashrc
  • .profile

and added as a 1st line an echo command to each of them, namely:

  • to .bash_login : echo "hi from .bash_login"
  • to .bash_profile : echo "hi from .bash_profile"
  • to .bashrc : echo "hi from .bashrc"
  • to .profile : echo "hi from .profile"

However if I run say

ssh name@host2 "ls ~/foo" I only see an output of "ls" but no echo output.

And why indeed it would echo? The man actually says:

When bash is started non-interactively, to run a shell script, for example, it looks > for the variable BASH_ENV in the environment, expands its value if it appears there, and uses the expanded value as the name of a file to read and execute. Bash behaves as if > the following command were executed: if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi but the value of the PATH variable is not used to search f

In other words, seems that according to the manual when bash is started non-interactively there is NO .-file that it runs by default.

One has to set it in an env. var-le "BASH_ENV" if she wants to source a file.

But then this seems to be an impossible thing to accomplish as... where one can set BASH_ENV or anything else as there is no file that is sourced?!?!?!?

This seems crazy. Am I the only man on Earth who needs to run bash non-interactively on a remote machine via ssh?

Please help. Thanks in advance.

  • 1
    What is your question? I see your problem, and I've already covered it fairly extensively in my answer to your previous question and in some previous posts I'd cited. But what are you asking now? – Gilles 'SO- stop being evil' Jun 20 '11 at 19:25
  • 1
    .. the character limit on questions is massively longer than anything you have entered. You must have tried to type a comment, which have a 600char limit. You need to go to your original question and edit the question to include the additional information, then ask for this one to to be delete. – Caleb Jun 20 '11 at 22:15
  • I went with "duplicate" since it's supposed to be an edit. Once you've edited the original post we can just delete this version – Michael Mrozek Jun 20 '11 at 23:58

You can choose to source one of those files and then run your .py script within the same ssh call by doing something like ssh user@host ". ./.dot-command && python_script.py

| improve this answer | |
  • Thanks, but this solution doesn't work for me (I explained it in the prev. post and forgot to add here). The reason is: this needs to be run for all shells (e.g. including tsch / sch) and all users. – bzdjamboo Jun 20 '11 at 19:46
  • Also important; assume that there are hundreds of such commands somewhere in the codebase; to use your solution I'll have to change the code everywhere. This is doable but seems to be so ugly. – bzdjamboo Jun 20 '11 at 19:47
  • The question stands; how to make bash source some file automatically when running a non-interactive session. There must be a way, right? – bzdjamboo Jun 20 '11 at 19:48
  • @bzdjamboo The way is to explicitly tell the shell to source a file, i.e. ssh hostname '. /path/to/script; actual-command-to-run'. If users need to be targets of your automatic runs, they must have a compatible shell. – Gilles 'SO- stop being evil' Jun 20 '11 at 22:41

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