Why isn't $KMVAR variable displayed in terminal with an echo command?

My expectation is that the command echo $KMVAR_textbar would show the value of the variable textbar.

By the way, i'm using this cool little app called TextBar to put text on the menubar. Works really well, http://www.richsomerfield.com/apps/.



Managed to get around it using alfred but still curious on why the variable didn't show on an echo command.

Two different scopes, I think. The shell environment invoked in a KM script action is not the same as that invoked by terminal.app. Slightly different shells, and they don’t automatically share environment variables.

I think you will see your variable if you run an echo command from a KM Execute Shell Script action.

(compare the output of the printenv command in both (terminal.app and KM shell action), including the values of variables like $PATH and $LANG, which are not automatically exported)

PS Peter’s update at the bottom of this thread is helpful:

1 Like

Oh I didn’t know about that, thank you.

In unix, the environment is passed down when you create a child process.

Something like this:

OS X starts up with process ID 1, which in OS X is /sbin/launchd. It sets up some sort of environment, including the environment variables. It then runs the loginwindow. loginwindow receives the environment passed down from launchd (possibly with changes). You then launch two applications, Keyboard Maestro and Terminal - each of these receive their environment variables from loginwindow. Keyboard Maestro launches Keyboard Maestro Engine which receives its environment from Keyboard Maestro. And Keyboard Maestro Engine runs your script, passing it the environment it has, together with all the KMVAR variables.

Terminal meanwhile has the environment passed to it from loginwindow - it knows nothing about the Keyboard Maestro variables and so when it runs a shell, that shell knows nothing about Keyboard Maestro variables.

You can retrieve the Keyboard Maestro variables via AppleScript requests to the Keyboard Maestro Engine, so you can get them from your script, but not in the form of KMVAR variables.

1 Like