1

Possible Duplicate:
Why does an SSH remote command get fewer environment variables then when run manually?

Running on Mac OS X 10.8.2

I'm running a perl script through SSH, in the perl script I query environment variables using $ENV{MY_VAR_NAME} and it works fine when run locally. But through SSH, all environment variables become unset.

I also tried to run

system("source ~/.bash_profile");

at the beginning of my script to no avail.

Any tips?

EDIT:

Rephrasing my question.

I have machine A and B. I ran my perl on machine B, trying to get the environment variables on B and it worked. Then I ssh from A to B running the same script, i.e., using this code

ssh user@B perl myscript.pl

This time the environment variables on B are all blank.

Any tips?

UPDATE:

I found that running the above script, ~/.bashrc on Machine B was invoked, but after setting environment variables in ~/.bashrc, run the above command again and still I don't see any environment variables.

Also, if my perl script contains only

echo $ENV{PATH}

Then I get

/usr/bin:/bin:/usr/sbin:/sbin
Community
  • 1
  • 1
kakyo
  • 10,460
  • 14
  • 76
  • 140
  • 1
    Not likely. A far more likely explanation is that you never set them in the first place. – ikegami Oct 09 '12 at 22:41
  • 4
    `system("source ~/.bash_profile");` will launch a bourne shell, which is told told to run a bash script and exit. Any changes to that shell's environment is not going to affect the Perl process's environment. – ikegami Oct 09 '12 at 22:44
  • 1
    Are you ssh'ing to a different machine and then running the program? In that case no, it won't have your environment variables, those are specific to each machine (actually each shell which is why your `system` command didn't work). Could you show what you mean by running Perl through ssh? – Schwern Oct 09 '12 at 23:53
  • Can someone up-vote my question if you think my edited description makes sense? – kakyo Oct 10 '12 at 00:30
  • @ikegami, I'm pretty sure that they are set since I ran the script on that machine and it sees them. – kakyo Oct 10 '12 at 00:31
  • @Schwern thanks, that does look like it. I'll try and report back. – kakyo Oct 10 '12 at 00:34
  • @kakyo, Let's put it this way: Perl does not unset environment variables. If they're not set, it's because they're not set in the parent process. – ikegami Oct 10 '12 at 00:56
  • Can we not mark it as a duplicate? I have tried many solutions on the "Why does an SSH remote command get fewer environment variables then when run manually?" None of them worked. – kakyo Oct 10 '12 at 14:38

2 Answers2

1

The environment variables which will get set when you use ssh to execute a command will depend on:

  • the login shell
  • the contents of the start up files for that shell

For example, when it start up bash may read any of the following files:

  • ~/.bash_profile
  • ~/.profile
  • ~/.bashrc
  • ~/.bash_login
  • /etc/profile

depending on factors such as whether the shell is considered to be a "login" shell, is the session an "interactive" session, etc.

You should find out which start up files are being executed when you use ssh.

You can do this by just putting echo here in XXXX in the various files, and perform the ssh command. (Note that ssh with no command may invoke different start up files than ssh with a command.)

For bash, consult the section INVOCATION in its man page for a description of which files are read when bash starts up.

ErikR
  • 51,541
  • 9
  • 73
  • 124
  • I'm running Mac OS X (bash 4.1). I found that when running my perl script using ssh, the invoked scirpt was ~/.bashrc. But then if I put some env vars in that script. It still does not set it by the time the perl script runs. – kakyo Oct 10 '12 at 03:30
  • Are those environment variables `export`-ed? – ErikR Oct 10 '12 at 14:40
  • I came to realize that maybe this is a Mac OS X specific issue because none of these login-shell scripts worked in any ways of calling, either using "source XXX && perl myscript" in the ssh line or putting the "source" command at the top of my script. Also tested with shell script version instead of perl. – kakyo Oct 10 '12 at 14:40
  • Put up a copy of the file `XXX` somewhere (i.e. pastbin.com) – ErikR Oct 10 '12 at 14:41
  • To round out this answer add: see the ssh manpage on environment set by ssh. ~/.ssh/environment is run then ssh invokes bash. As already said if not interactive then bash will run different startup files. – gaoithe Mar 10 '16 at 18:02
  • Note that putting echo commands in startup files is fine but can cause unexpected trouble :-o. e.g. ssh port forward commands can silently fail if a startup script produces output. – gaoithe Mar 10 '16 at 18:04
0

The trick is to run .bash_profile and the perl script under the same remote shell session:

ssh user@B 'source ~/.bash_profile && perl myscript.pl'
salva
  • 9,943
  • 4
  • 29
  • 57