Why does Windows `perl` fail to acquire environment variables when run under `sh` over SSH?

Viewed 35

Overview

I have a puzzling problem where a command I run behaves differently when run over SSH compared to run directly on the machine (via RDP). I have narrowed the issue to a Perl script in which an environment variable (accessed via $ENV{VARNAME}) is not set if the script is run within an sh shell.

My testing scripts and results are shown below. Why would Perl, when run within sh, behave differently over SSH (it cannot access environment variables).

Program details

These are the programs I'm using:

  • sh: GNU bash, version 4.4.23(1)-release (x86_64-pc-msys); released with git (2.30.0.windows.1)
  • perl: v5.8.4 built for MSWin32-x86-multi-thread
  • ssh client: OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
  • ssh server: Bitvise SSH Server 5.60

Process

I open two prompts simultaneously; one over SSH, one over RDP. The environments are not exactly the same to start, so I delete some variables at each prompt, then dump the environment to a file for later:

  • on SSH:

    C:\Users\me>set PROMPT=$G
    >set SSH_CLIENT=
    >set SSH_CONNECTION=
    >set SSHWINGROUP=
    >set WINSSHDGROUP=
    >set HOME=
    >env | sort > env.ssh.txt
    
  • on RDP:

    C:\Users\me>set PROMPT=$G
    >set CLIENTNAME=
    >set SESSIONNAME=
    >set TEMP=C:\Users\me\AppData\Local\Temp
    >set TMP=C:\Users\me\AppData\Local\Temp
    >env | sort > env.rdp.txt
    

Environment check

The environments are the same!

>diff env.rdp.txt env.ssh.txt
>

Test

Now I test both SSH and RDP by doing the following:

  • set a variable MYVAR to BCD in the cmd prompt
  • run Perl script in cmd that prints A$ENV{MYVAR}E
    • expecting ABCDE to stdout
  • start sh, then run the equivalent Perl script

Note that:

  • over RDP, the Perl script under sh outputs ABCDE
  • over SSH, the Perl script under sh outputs AE
    • i.e., Perl does not acquire the environment!

Test SSH

>set MYVAR=BCD
>perl -e "print ""A$ENV{MYVAR}E\n"""
ABCDE
>sh
$ echo $MYVAR
BCD
$ perl -e 'print "A$ENV{MYVAR}E\n"'
AE
$ 

Test RDP

>set MYVAR=BCD
>perl -e "print ""A$ENV{MYVAR}E\n"""
ABCDE
>sh
$ echo $MYVAR
BCD
$ perl -e 'print "A$ENV{MYVAR}E\n"'
ABCDE
$ 
0 Answers
Related