The Execute a Shell Script action executes a specified shell script, either from a file or from the script embedded in the Action.
To execute the script from an external file, choose the “Execute Script File” in the choices for script location.
Use this format: $KMVAR_[variable_name]
where [variable_name] is the name of your Keyboard Maestro Variable,
but with spaces in the name replaced with underscores (‗
)
For example: “File Name” would be formated as “$KMVAR_File_Name”.
If your variable already has underscores, you can use as is.
It is generally a good idea to surround your variable with double quotes in a shell script, for example
cat "$KMVAR_File_Path"
so that it will work properly if the path contains spaces. Make sure the path is a full path, not a path relative to your home directory starting with ~ as that will not be expanded within the double quoted string. So if it might contain a ~ at the front, use the Filter Variable, Expand tilde (~) paths action to expand it to a full path first.
The Action (gear) menu includes the following options:
The results of a shell script can be:
For example, you could have a shell script date display briefly in the Notification Center every hour, or use a hot key to type the results directly into your text editor.
You can also use the clipboard by piping from pbpaste and to pbcopy.
Proper quoting of strings in a Shell Script is often a challenge.
For a discussion on how to quote a string, see this Keyboard Maestro Forum post by @PeterNLewis.
In essence, the default path in a Keyboard Maestro Execute Shell Script is the base path for the system:
/usr/bin:/bin:/usr/sbin:/sbin
$PATH
environment variable you may have set in Terminal is NOT automatically used.
: [@JMichaelTX: I propose to remove the below paragraph since the system path is now provided above.]
The default PATH for the system will generally be /usr/bin:/bin:/usr/sbin:/sbin
- that is the script will search for tools in the /usr/bin
directory, then in the /bin
directory, then /usr/sbin
and finally /sbin
. Only tools installed by the system will be in these directories - any tools you have installed will almost certainly be elsewhere and so not found unless you either configure the PATH environment variable within Keyboard Maestro (by setting the ENV_PATH
Keyboard Maestro variable or explicitly setting the PATH environment variable within each Execute Shell Script action) or use the full path of the command (eg, /usr/local/bin/exiftool
).
How To Set a Path
$PATH
environment variable within each Execute Shell Script action.ENV_PATH
(the Keyboard Maestro Path Variable) prior to the Execute Shell Script Action.$PATH
works in the Terminal app.ENV_PATH
Variable is set, it will remain in your Keyboard Maestro Variable set (until/unless you delete it), available to every Execute Shell Script Action that you might use in the future. So you don't need to set it in every Macro.ENV_
at the front.ENV_PERL5LIB
that will [which? use OR be set to] the PERL5LIB
environment variable for each Execute Shell Script.ENV_PERL5LIB
variable need to be set explicitly to a path? OR, does it somehow get the path from the PERL5LIB
environment variable?]Setting the ENV_PATH Keyboard Maestro Variable
A shell script path that will work for many users is:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Of course, if you have tools or files you installed elsewhere, then you would need to include the path to those in this path statement, IF you plan to use any of those tools/files in a Execute Shell Script Action.
You can create the ENV_PATH
Variable one time using the Variable Preferences Pane of the Keyboard Maestro Preferences.
ENV_PATH
and set its value.
For a good discussion about this see:
When you execute a shell script from other apps, like Terminal.app, the shell does NOT have access to the Keyboard Maestro Engine environment, and thus it does NOT know anything about Keyboard Maestro Variables.
In order to access Keyboard Maestro Variables in these scripts, you must use a tool like osascript.
⚠️ Note that you do NOT need to replace spaces with underscores in your Variable name in this case, since you are ultimately using AppleScript in the Shell Script.
Here is a simple example that works with Keyboard Maestro Ver 6+ to get a variable:
osascript -e 'tell application "Keyboard Maestro Engine" to get value of variable "My KM Var"'
If you are using Keyboard Maestro 7.1+ you can use the new simpler command to get a variable:
osascript -e 'tell application "Keyboard Maestro Engine" to getvariable "My KM Var"'
To set a variable (and create if needed) with Ver 7.1+ you can use this:
osascript -e 'tell application "Keyboard Maestro Engine" to setvariable "My KM Var" to "Some new value"'
Here is a more comprehensive example, providing error handling:
read -r -d '' theAppleScript <<'EOF' tell application "Keyboard Maestro Engine" set kmVarName to "My KM Var" tell variable kmVarName if it exists then return its value else return "Error → Keyboard Maestro variable '" & kmVarName & "' does not exist!" end if end tell end tell EOF osascript -e "$theAppleScript";
Keywords: Quote String, Bash, Shell Scripting