Feature Request: "Run Shell Script" Uses Same Shell as the Terminal Shell

@peternlewis It'd make using KM and running shell scripts 100x more straightforward.

Thanks

I think doing this by default will cause more problems. Apple has switched the default shell several times, most recently (in Catalina I think?) to zsh. An OS upgrade can break your scripts.

/bin/sh is the traditional "least common denominator" default for most Unixes and Linux, and all of these, including KM allow you to easily override this with a single line using the hash-bang specifier.

Example macro below launches 3 different shells. $$ is the pid of the current shell so ps -p $$ will give you the command line of the shell, including which shell was launched.

By including the hash-bang I can force a particular shell, even if Apple changes what the default is later.

Macro Download

Shell Script test.kmmacros (2.7 KB)

1 Like

Hey @hello,

I think that would violate Apple's User Interface Guidelines.

I believe the default macOS system $PATH is:

/usr/bin:/bin:/usr/sbin:/sbin

That's the same as Keyboard Maestro's default $PATH:

/usr/bin:/bin:/usr/sbin:/sbin:

And the same as AppleScript's default $PATH:

set shCmdStr to "echo $PATH"
do shell script shCmdStr

--> "/usr/bin:/bin:/usr/sbin:/sbin"

All you have to do to replicate the altered system path on your machine in Keyboard Maestro is to:

Paste echo $PATH | pbcopy in the Terminal.app and run it.

Then paste the result into a variable in Keyboard Maestro named ENV_PATH.

Here's mine by way of example:

/opt/local/bin:/opt/local/sbin:/Users/chris/perl5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin

Since this setting is can be deleted deliberately or accidentally, and since I need to have a default path occasionally for testing – I have a macro to recreate it for me.

Make ENV_PATH Variable.kmmacros (1.8 KB)

Here's a significantly upgraded version of the macro that uses the Terminal.app to acquire the $PATH.

-Chris

1 Like

Thanks, that helped.
I hope one day KM's shell works exactly like Terminal, to avoid the need to create variables, and scripts not running.

It won't and can't.

For one thing the Terminal is an interactive environment, and Keyboard Maestro's Execute Shell Script actions are Data-In ⇢ Data-Out black-boxes.

If you're using a .profile, or .bashrc, or similar file you can use the source command in your Execute a Shell Script action to load your custom Terminal environment.

This will load up all the aliases, functions, and other changes you've made via the given terminal customization file – including any changes made to the path.

You have a great deal of freedom to customize Keyboard Maestro to your preferences – like many things you just have to know how.

-Chris

2 Likes

As @ccstone notes, a script run by Keyboard Maestro is run in the same way a script would run without any external environment.

It cannot run in the same way as Terminal runs for a whole bunch of reasons. And it should not. Scripts run in Terminal potentially include a whole bunch of customisation that is designed to make your life ion the Terminal more pleasant, but at the same time make the script environment far less predictable.

Scripts run in Keyboard Maestro are run with /bin/sh and with minimal environmental changes except for explicit ones.

Otherwise, the day you decide you want to switch default shells, or install some fancy shell extension, or change how your prompt is displayed, or whatever, is the day half your Keyboard Maestro scripts break in weird ways.

You should write your scripts with minimal external requirements, and with explicit requirements in the script when necessary.

3 Likes