How Can I Execute KM Shell Script without any KM Variables in the Shell Environment?

Is there an option to execute shell script without any Keyboard Maestro variables?

I'm trying to call an external terminal (kitty) with several options, but do not want/need the keyboard maestro variables. I've been scouring the forum and wiki, but cannot find any info on that.

The KM Execute a Shell Script action does NOT require the use of any KM Variables. You should be able to execute any command line provided you give it the proper path.

Thanks for the response.

Currently, I am trying to call the external terminal without any keyboard maestro environment variables. The reason being, I need to pass in some flags and options, so I cannot simply use the built-in "activate a certain program".

However, when I run the shell script, all the keyboard maestro variables are exported into the sub-shell: if I echo $KMVAR_kmvar, I get back the predefined string.

On the off chance, is there any option to run a script without exporting the environment variables? If not, are there other ways to call an external shell with flags/parameters without opening a Terminal/iTerm/kitty instance itself?

Why do you care that the KM Variables are in the shell environment?
Have you tried running "kitty" in an Execute a Shell Script action?

Note that the KM Execute Shell Script does NOT allow any user interaction in the shell.

Also, note the following:

The Execute a Shell Script action executes a specified shell script using /bin/sh, either from a file or from the script embedded in the action.

Important:

  • It is best to always specify the language using the #! at the front of the script.

  • The script is executed with the sh shell, so if you want to use another language, or use any special kinds of shell-specific syntax, then you should specify the language.

  • For more details and examples, see How to Use Shebang at Top of Shell Script

Not that I know of. Perhaps @peternlewis can comment/clarify.

Since you asked a new/different question, I have moved your post and replies to a new Forum Topic.

Why do you care? The shell often has lots of irrelevant environment variables in it - unless you reference them, or unless you actually run out of environment variable space why would it matter? Environment variables don't affect anything unless they are explicitly used for something or have a pre-defined meaning (which KMVAR variables do not).

But if you really really want to get rid of them, do it in the script - go through all the environment variables and unset all the KMVAR ones (I don't have shell code to do that offhand but it cannot be that hard).

2 Likes

Thank you both for pointing me in the right direction.

As I want usually want at least one terminal (interactive) instance open, I wanted to mimic activate kitty.app without the special KM variables in my main interactive session. Personal preference, I guess, aligning with the philosophy of using local variables in KM as well.

Currently, the other alternative to unsetting variables I see is to just open a terminal, run my command (/usr/local/bin/kitty --bunch-of-flags), but I can't close the parent shell, otherwise it would terminate my kitty instance. I know it's going off topic now, but Google searches led me to the bg, disown commands, but honestly I'm at my limits.

Which leads me to a general, final question: is there a native way to run background scripts preferably via hotkeys?

I don't really understand wanting the KMVAR variables gone either, but if it's important to you, it can be done.

In bash this unset command will automatically remove all variables that start with KMVAR:

unset $(compgen -v KMVAR)

Which means you could do something like this to launch kitty

#!/usr/bin/env bash

unset $(compgen -v KMVAR)

/usr/local/bin/kitty --start-as=normal

exit 0

THE MOST IMPORTANT THING is to make sure that the shell script is set to run asynchronously or else the Keyboard Maestro menu bar item will continue to animate as long as your kitty is alive`.

See screenshot below:

Note that my system may have a different /path/to/kitty than yours. Use whatever is correct for your computer. You would want to add your own flags as well, obviously.

I hope this helps.

3 Likes

Wow thank you for this detailed response!
Picked up a couple of new syntax for further learning, even "fixed" the swirling icon that I just assumed was the engine constantly listening :rofl: