MacOS – Setting environment variable for sudo in Yosemite

launchdmacosterminal

I followed this similar question to set my environment variables in Yosemite: https://stackoverflow.com/questions/25385934/setting-environment-variables-via-launchd-conf-no-longer-works-in-os-x-yosemite/26179907#26179907

I want to do the same thing, but for sudo

do shell script "sudo launchctl setenv variablename value" with administrator privileges

but somehow, it does not show if I go in a terminal afterwards and run the command:

sudo launchctl getenv variablename

What am I doing wrong ?

Best Answer

Your first line of code is wrong. If you say sudo and with administrator privileges you are doing the same thing twice. If you use the terminal, as you can see, the following works:

$ sudo launchctl setenv TEST true
Password:<you have to insert your password here>
$ sudo launchctl getenv TEST
true

You could unset the variable with the command:

$ sudo launchctl unsetenv TEST

You don't need an applescript to do that.