I have a script on a server-A


#!/bin/bash -l
echo "script-A.sh" | change-environment.sh

When I ssh onto server-A and execute it, it works fine. However, when I

ssh user@server-A ./script-A.sh

Script-A executes, but throws an undefined variable error in change-environment.sh.

change-environment.sh runs in the c shell(I have no control over the script so the method I have used is about the only way I can use it), but everything else is in bash.

Had found a similar question at I can run a script locally, but cannot do "ssh HOSTNAME /path/to/script.sh". However, there was no solution to the issue and it was a year old.

  • Did you check the environment variables? They are a most probable culprit, because the environment differs slightly if you don't SSH into another computer, but run a command directly. Run the export command while logged in, as well as directly from the ssh command line (instead of script-A.sh), and compare the results. – Lacek Oct 1 '12 at 9:32
  • Thanks for the reply.. Only the usr/X11/bin and usr/X11R6/bin are missing when I do the same thing from the command. – neorg Oct 1 '12 at 10:10
  • This is also as common as dirt for scripts called from crontab. It's not the usual full environment when such scripts kick off unless extra measures are taken. – Magellan Oct 3 '12 at 5:51
  • I solved this issue ultimately by making changes to the change-environment script and adding the missing environment there. Even the accepted solution is not exactly complete bcoz even though they point to the root of the problem (the environment is not getting set), I believe there should still be a more generic solution... atleast I hope so... Thank you Adrian,user694971 and Martin Baulig for the suggestions... – neorg Oct 4 '12 at 9:36

When using ssh with a command, it may not actually create a login shell even if your script uses #!/bin/bash -l.

Try ssh user@server-A bash -l, then run your script. You can also create something like


Then run it after logging in manually (ssh user@server-A) and when using ssh with a command (ssh user@server-A name-of-that-script.sh).

For instance, on Mac OS X, using ssh martin@nathan bash -l does not even set PS1, whereas as normal ssh martin@nathan interactive shell does.

Another important difference is that a pseudo-terminal is only allocated when running interactively (logging in without a command).


You script would likely need a terminal (tty). You can simply run ssh and force pseudo terminal and it should run just fine

ssh -tt remote_host "your_script"


You really need to compare you environments, .profile helps you to keep them the same. Why don't you walk over your code and see which env vars it depends on? Then you can fix these env vars inside your script-A. By the way, I also advice you to invoke bash scripts like this while debugging:

bash -v


  • The change-environment script makes a lot of changes to the environment. So it is difficult to keep track of all the changes. – neorg Oct 1 '12 at 10:22
  • Oh ok... Maybe you can try to concentrate on env vars that are not self contained. A lot of exports often look like export PATH=some stuff ...:$PATH, i.e. they have a strong dependency on the old env. (In this case on the old $PATH var) Is it possible to remove deps to the old env? – user694971 Oct 1 '12 at 10:28
  • Also I have added the -l option to bash... so it should be sourcing the .profile automatically I guess – neorg Oct 1 '12 at 11:18
  • .profile is always sources AFAIK - .bashrc is not – user694971 Oct 1 '12 at 15:28

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.