Mac Environmental Variables and SSH

Mac OS has a mechanism whereby Environmental Variables can be set for all of your processes by using a ~/.MacOSX/environment.plist file. BASH, my favorite shell, also has a separate mechanism via a .bash_profile file in your home directory. If you invoke bash via, you’re golden, because the Env. Vars. will be inherited from the parent process.

However, if you SSH into your machine, env vars set in ~/.MacOSX/environment.plist won’t be set. This is a source of frustration, as I use perforce, and its configuration is set in Env Vars.

I developed this workaround to scan ~/.MacOSX/environment.plist when I detect that a login is remote SSH connection. It then manually adds all the values to the new bash process.

if test "" != "$SSH_CLIENT" ; then
perl -e 'while(<>){ if (m|(.+)|){ print "export $1="; } if (m|(.+)|){ print "$1\n"; } }' .MacOSX/environment.plist > /tmp/$EUID.vars
. /tmp/$EUID.vars
rm /tmp/$EUID.vars

Once again, the perl swiss army knife shows its utility, and its ugly face.

Leave a Reply