Display current value of a single environment variable

Is simple way in Keyboard Maestro to read/capture a single environment variable? I can see that overriding an env is trivial, using the %ENV_…% pattern, but the only documentation I can seem to find discusses use of the env command, which is several steps removed.

So far, the answer seems to be either "Build your own script and caputre the result," which I can do, but I'm wondering if there's a simpler way using non-scripting Keyboard Maestro features.

How was the shell script created? You should provide an example. If you are using KM's "Execute Shell Script" action to create the shell, then there is no way to access the variables in the shell because you can't run any code in the macro until the shell terminates, at which point all the variables are erased. You can work around this by returning a result from the shell (through several different mechanisms) but you haven't given an example of what kind of result you want. A number? A string? A file? Before I can help, I need to know more about what you are trying to do. Either show the code you have or explain the problem you are trying to solve.

"Environment variable" is a pretty broad term -- how you read one will depend on what sort of thing you are talking about.

Have you got some examples of what you want to do?

TL;DR—Thanks, both of you. By environment variable, I'm referring to the results of running env in Terminal. Please read the last sentence below before doing any coding work on my behalf. :grimacing:

Broader Context
I haven't included an example because this question came up while I was just exploring KM, trying to understand how things work. In particular, I'm doing a deep dive into the Pause Until Conditions Met action, which has an Environment Variable Condition option.

I figured, ok, to test this out, I need a known environment variable, and I need to know what it's currently set to. I figured there'd be an easy way to do that with KM, but instead went down a rabbit hole which, on the plus side, taught me about the %ENV_…% pattern, which could come in handy some day. Of course, if I'm going to override an environment variable, I'll want to first make sure I know what's in it, at least during testing, so the same question remains.

Of course, it's hard to prove a negative, so the best answer may be, "Not that I know of." ¯\_(ツ)_/¯

Since I don't actually have a production use for this, I hope y'all will only post solutions if you either have one premade that future seekers could use, or you just feel like solving a random puzzle.

That's why I asked "How was the shell script created?" I was suspicious that you wanted the variable in a Terminal, but didn't want to postulate that aloud in case I was wrong. The Terminal app can run multiple shells, and none of those shells are child processes of the KM Engine, so there's no direct way to get a variable from those shells. How would KM know which Terminal window/shell is the one that you want to get a value from? The frontmost window? If so, you could have KM send a string to the front most window that would feed the contents of the variable into a file so that KM could read the contents of the file to retrieve the value. Do you want help with that? It should work perfectly well.

Hmm, I see. I've asked the wrong question. This is clearer, I think:

The documentation mentions the system environment variables that make up Keyboard Maestro Engine's environment, but doesn't talk about where they come from, how to discover them, or when they could be helpful.

How does one discover the values that can be used with either the Environment Variable Condition option, and/or with %ENV_…%?

As for your first question, I'm pondering it. As for your second question:

What is "%ENV..."? There is no token starting with "ENV". Please give me an example macro using this feature or a web page that describes which feature you are talking about.

https://wiki.keyboardmaestro.com/Tokens

As for your first question, I'm pretty sure you can see the list of all environment variables that the KM Engine uses with this action:

image

The result of that command will also add any global variables, unless you remove them by clicking on the down arrow to the left of the word "printenv" and specify "Include No Variables."

As for "why would you want to know this information", I can't say why you would want to know this, but there are some potentially useful variables in there like SHELL, USER, PATH, PWD, LOGNAME, etc.

1 Like

The KM wiki talks about environment variables here manual:Scripting [Keyboard Maestro Wiki]

The last line says:
"Variables whose names start with “ENV_” override regular environment variables (eg “ENV_PATH” overrides the regular “PATH” environment variable)."

1 Like

Note that this isn't a pattern as such, and not a token. ENV_ is a special prefix to a variable name that, as @tiffle says, denotes it should override any regular environment variable with the same name (minus prefix, obvs).

So any variable you name ENV_<whatever> will be available as an environment variable when you "Execute Shell Script", either as an additional variable or overriding an existing one. As @Airy said, you can printenv those to see the "defaults", and it's easy to demo the override:

ENV_ Demo.kmmacros (4.7 KB)

Image

2 Likes

KM Process Environment (Keys and Values) as JSON.kmmacros (3.3 KB)


Expand disclosure triangle to view JS source
return JSON.stringify(
    ObjC.deepUnwrap(
        $.NSProcessInfo.processInfo.environment
    ),
    null, 2
);
1 Like

image

1 Like

Thanks, everyone! I understand this much better now!

Since this post ended up covering several related topics, there are really more solutions than the forum system allows me to recognize.

Summary, for the benefit of future readers…

1 Like

Note that to the left of the code field in the Execute JavaScript for Automation action, there is a small chevron providing a menu from which you can choose which (if any) of your Keyboard Maestro Variable names and values are exported to the environment.


1 Like