Please Help Me Untangle My Bash/Shell Issue

Hi everyone, I used a script to access the API of http://onetimesecret.com/. The script's origin lies here: GitHub - eengstrom/onetimesecret-bash: Command-line tool and BASH API client for One-Time Secret https://onetimesecret.com/

After upgrading to a new machine and Ventura it no longer worked, stating it needed at least bash 4.0. So, I installed bash via homebrew and at least from within the terminal I can use the script and it works fine.

Making it work with the "Execute Shell Script" action is another deal, though. I read the wiki as well as the corresponding posts, but I seem to miss something. Whatever I do, it doesn't help. Any ideas where I need to look more or where the issue might be is greatly appreciated.


Some context information

Executing #!/bin/sh echo "$SHELL" always returns "/bin/zsh". No matter if I set the variable "ENV_PATH" or not. I tried setting it to "/opt/homebrew/bin/bash". At this point, if I try to run the script the KM notification says:"bash command not found".

In the terminal bash --version brings me GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0) and the script itself works fine.

If, back in KM, I empty the ENV_Path and add #!/bin/bash the notification gives me the compatibility issue due to bash being outdated. If I add back the correct path, I get the command bash not found information.

I feel a bit confused and don't really know where to look next. Any ideas? :pray:

echo $SHELL returns your "default" shell, and has nothing to do with which shell you are actually using. Instead, use $0:

iMac-3:Desktop nigel$ echo $SHELL
/bin/bash
iMac-3:Desktop nigel$ zsh
user-168% echo $SHELL
/bin/bash
user-168% echo $0
zsh
user-168% 

But that's by-the-by. You'll need to start your "Execute Shell Script" action's script with a "shebang" line pointing at your desired bash -- so something like

#!/opt/homebrew/bin/bash
# script goes here...

You may also need to add /opt/homebrew/binto KM's ENV_PATH, depending on whether or not the script calls other brew-installed items.

That should point you in the right direction -- if not, post your actual macro (or at least the "Execute Shell Script" action).

Thanks for your reply. I tried what you suggested. So far, it has no effect. I don't even get a notification.

ENV_PATH = /opt/homebrew/bin

Because the script you are running also defaults to your non-brew bash -- change the first line of that instead of putting the shebang in the action.

Do you put any output on the Desktop? If not then there's no need to change directory and your action is just one line:

/Users/woody/Desktop/ots.bash share --secret "foo" passphrase="bar"

...while the first line of ~/Dekstop/ots.bash should be changed to

#!/opt/homebrew/bin/bash

...so the interpreter uses the right version.

(At the moment you're saying "Hey, KM! Use bash v5 to change directory and run this script". bash5 opens the file, reads the first (shebang) line and is told that the following script should be interpreted by your default bash v3. Since I'm working blind I'm suggesting to explicitly run this script with v5 rather than changing things so all KM bash scripts use v5, because who knows what that might break!)

Hey Guys,

This is how I handle that.

Execute a Shell Script @Working.kmmacros (1.9 KB)
Keyboard Maestro Export

5.2.9(1)-release

I installed BASH 5 with MacPorts rather that Homebrew, but the principle should be the same.

I do have to set my ENV_PATH variable correctly for this to work, and mine encompasses quite a lot and thus is pretty complex:

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

I used to do this manually, but I've accidentally deleted it a few times and have codified the process in a macro.

Here's a macro that creates a ENV_PATH Keyboard Maestro variable for you using your Terminal to extract your system's current assigned $PATH string.

Replicate $PATH From Your Terminal.app Environment to Keyboard Maestro

-Chris

1 Like

My worry is always that changing the default bash might break other things that don't want v5... Only OP knows which will be more painful -- individually setting those things that need v5 or individually setting those things that need v3.

(Yes, I've seen a self-proclaimed Bioinformatics guru fubar his own project pipeline doing this. Had a hard time getting out of the room before bursting into laughter :wink: )

1 Like

I get it.

I nearly never use the non stock shell in Keyboard Maestro, unless I need its features.

Thank you very much @Nige_S and @ccstone - both of your replies helped me solve the problem.

The hint changing the shebang within the script totally made sense once I read it. Before that, it didn't really cross my mind. Once I did that I got a dependency error which was kind of weird because I did install it. This is where the macro posted by @ccstone really helped me set the ENV_PATH variable correctly.

After combining both, the script finally works. Yay! :tada:
Thanks again, guys. I greatly appreciate your help!


EDIT: @ccstone Within your macro I had to replace the first menu action with "Settings" because I got a small notice saying the menu item "Preferences" couldn't be found.

I't's still “Preferences” on Mojave. I guess that automatically changes on later systems.

Would you post an image of that specific action please. I'll either add a note to the macro, or I'll fix it with a regular expression.

TIA

Of course. Here you go. This works on Ventura with KM 10.2. I don't know, but could this not also be solved by invoking "Command + ," which universally opens the preferences?

1 Like

That would probably work, but keystrokes are less secure than menu selections – so I prefer to use select-menu-actions when possible

Ah, good to know. :+1:

1 Like