I have wasted most of the day trying to get some simple environment variables set that will be visible to GUI apps along with shell variables. I have tried virtually everything I've found on the web, but I can get a variable set with launchctl to visible in bash. I have a script that runs as a login item that does a simple launchctl setenv FOO BAR
. When I type launchctl getenv FOO
it returns BAR
. If I type echo $FOO
I get an empty string. I can't believe apple would something this simple so hard. I must be missing something. Am I?
1 Answer
When you type launchctl setenv FOO BAR
in Terminal you are setting the variable in launchd
environment. You will have to restart Terminal (all its processes) to see the change. launchd
will pass your new variable while starting Terminal and it will be visible in it's new instance running echo $FOO
. The same applies to all applications.
-
That's what I was expecting to happen but it doesn't. If I set the variable in a terminal window, close the window and open a new terminal, 'echo $FOO' still returns an empty string. I've even got a small application I created with script editor that is run at startup setting the variable. Same result, always empty from bash command line. Been pulling my hair out for most of the day trying figure out why....– cpeddieCommented Mar 30, 2015 at 22:11
-
1I just tried it and it works here. Be sure to close the whole app, not only window.– bafCommented Mar 30, 2015 at 22:16
-
My bad. I was only closing the window and not the entire app. :( Works like a charm now. THANKS!– cpeddieCommented Mar 30, 2015 at 22:37
-
Just a FYI. On macOS Monterey, I just tried by running the command
launchctl setenv FOO BAR2
but when I try to echo it withecho $FOO
, nothing shows up. Regardless if I try to echo in the same terminal or a new terminal. Commented Sep 30, 2022 at 17:34
killall Dock
might help.