Execute Shell Script with no results returns an error

I have an Execute Shell Script action that sets a local variable. If the commands in the script return nothing (an empty string), then the action errors out. For example: this works (the 'ps' command returns text that contains the string "root"):

ps -cA -o user,pid,command | grep "root"

but this version causes an error ("garbage" is not found in the 'ps' result):

ps -cA -o user,pid,command | grep "garbage"

An empty (null) string should be as valid as any other string. If I add an 'echo x' before the 'ps', the action still errors out. Why does a null string result cause an error?

Hey Jeffry,

The shell script isn't posting a null string per se – it's exit status indicates an error condition, since the command failed.

Keyboard Maestro has tools for managing this in the gear menu of the Execute a Shell Script action.


Execute Shell Script – Allow Error to Flow Through v1.00.kmmacros (8.0 KB)

You can also control the result from within the shell script itself:

Execute a Shell Script.kmactions (856 B)


That was a great answer and I discovered this exact problem myself independently and diganosed correctly myself. However I don't want this to end here. There's more clarification to be requested on this topic. Namely, why does KM generate error messages when the shell does not? For example, in a shell this does not generate an error:

echo q | grep z

But in a KM macro, it generates an error. I realize (now, after 8 hours of troubleshooting) what's happening, but why does KM make an "error" about something that the shell does not? Why is the behaviour inconsistent with the shell? I probably spent an entire day figuring this problem out for myself. I wouldn't have had to spend any of that time if the KM Execute Shell script action behaved exactly as the macOS shells do, which is to ignore this error. How is it even an error? In the man pages for grep it says that the command returns a 1 if there are no results. The shell doesn't report this as an error, so why does KM? This is inconsistent. I don't know who is right, but it's inconsistent.

I don't know how my tone of voice sounds, but I'm just trying to make a strong case, not sound unpleasant.

I'm here to petition that either (a) KM should interpret the results the same way that the shell does, and should not report an error or (b) if these behaviours are not aligned, then the KM documentation for the Execute Shell Script action should explicitly state that it behaves differently from the shell by returning any value>0 as an error, which the shell does not do. I see no mention in this following documentation about which return values generate errors or how KM differs from the shell in the way it reports them. Why can't the documentation mention this divergent behaviour? It should be mentioned at the top of the page under the heading "Important."