[SOLVED] Variable not working with shell

I have set this variable:

When I use it with an IF THEN action using a script, it doesn't work:

I tried with and without the double quotes, same results.

When I use the path itself, it works:

This is a simple action

How can the line count ever be more than one for a file or folder in the same parent directory? I don't see any way your condition is ever true, because the function is either going to return 0 (no file/folder matches) or 1 (a file/folder matches).


I'm a bit confused for 2 reasons:

My action is saying:
If the number of files in a folder is bigger than 1, do this. Otherwise (which will be 1 or 0), do that.
I'm confused about your thought process here...

Also, as I mentioned, I tested it and it works when I'm using the full path. I run it with 2 files inside the folder and it shows me as True. I remove one of them, so I only have 1 file, run it, and I get False.
But when I use the variable, no matter what I do, I always get one of the results.
So there's something with the variables not working, not how the action is set up.

So "Song 01" is a folder? Sorry, my mistake, I assumed it was a file. If you put that variable/command in a normal Shell Script Action, does it return the proper results?

Just by itself, Do Shell Script, show results in window.


Yes, that's a folder. Then inside I will have either just 1 Photoshop file (if that happens I want KM to open that file), if it contains the Photoshop file along with a bunch of PNG images, it will go to folder Song 02 and check the same thing until it finds a folder with just 1 Photoshop file and opens it.

Yes, if it's one file, it shows 1, if it's 2, it shows 2.
So why would the action IF THEN not work...?
So weird

So here's what I experience:
If I use the variable inside the double quotes, the result is True, no matter how many files are inside
ls "$KMVAR_Local__dataSetFolder01" | wc -l

If I use it without quotes
ls $KMVAR_Local__dataSetFolder01 | wc -l
is shows me as False, no matter how many files are in the folder

Check if folder is empty copy.kmmacros (26.8 KB)

Check it yourself. Let me know if you experience the same thing

Very strange, as this works on my Mac:

It shows "it was 10" as that is how many files/folders are in my Documents folder. Looking at your macro now.


Both versions of that work fine on my Mac, but definitely only with the double quotes. Without them, it will fail: Open the log file, and you'll see a bunch of messages about "No such file or directory," because the parsing will break at the first space, and try to treat each bit as a folder, all of which will generate errors as they don't exist.

Once you put double quotes around it, try again and then look at the log file again - anything there?

Help > Open Logs Folder, and you want to open the Engine.log. I use tail in Terminal to watch it in real time:

tail -f ~/Library/Logs/Keyboard\ Maestro/Engine.log


The log always says the same thing, that the directory doesn't exist, with or without quotes.

Full path:
2023-11-23 01:04:58 Execute macro “Check if folder is empty copy” from trigger Duplicate Macro Palette

Variable with or without quotes:
2023-11-23 01:05:19 Numeric condition failed to evaluate “ls: : No such file or directory…”

But even though they both get the same error, the notifications are different.
One gets the "more than 1" notification, and the other gets the "1" notification.

Go figure...

There's got to be a very basic explanation here, but I just can't see it. I use that construct a ton, and it "just works."

Maybe there's somehow an invisible character in the KMVAR version? That doesn't make any sense, but it's about all I got. You could try copy/pasting the path from the working shell script line into the set variable action. But I doubt that'll help; it's something simpler that I'm just not seeing.


I looked at it too, and I can't see it. If the goal is just to get the number of files in a folder, there are other ways to do it. Here's one way to do it using a single KM loop containing one action: (the Display action isn't required, it just displays the total)


But if the goal is to figure out why his action doesn't work, I'm still pondering that.

Ok I found the issue. Don't ask me why this is happening, but I tested all the options.
I created a new macro, new actions, etc. There's no issue with extra characters or anything. I typed everything manually, copied and pasted the variable name to avoid typos, etc.

Set Var action - enabled
Shell action - enabled
If Then - disabled (this is why it's weird, because it's affecting it, I have no clue why)

If the condition is set to one of those last ones (any of these 6 options)
I always see the error, even if the action is disabled, as I mentioned.
So there's something weird going on with those conditions, but also with the fact that the action is disabled and it's still affecting the engine.

For example, No Error here:

2023-11-23 02:15:14 Execute macro “New test” from trigger Editor

When I change it, error (on any of those last options

2023-11-23 02:16:24 Numeric condition failed to evaluate “ls: : No such file or directory…”

So again, I have no clue why those options would show an error and the others wouldn't, and why is this being affected if the action is disabled?

@peternlewis any idea what's going on here?

Something is really weird... I tried with the >>full path<< and this time I didn't add any condition value, like this:

It seems that the engine is still evaluating it
2023-11-23 02:21:55 Numeric condition failed to evaluate value “”

As soon as I added a value:

even though it's using one of those 6 options at the bottom, no error:

Go figure...

Why wouldn't it still be evaluating it? Even if the field is blank, it's going to evaluate the condition when it runs the action and then since “” is an invalid number, it is going to error.

2023-11-23 02:21:55 Numeric condition failed to evaluate value “”

Th issue you are having is that by default the shell script condition does not include any variables.

In the popup menu on the left of the script, ensure that the variable you are using is included.

Because the action is disabled. I would assume that when an action is disabled, it's the same as not being there.

I didn't know about this menu.
It is in fact, by default, set to not include variables, but I see that the Execute Shell is set to include. Is there a reason for both having a different default?
If not, wouldn't it be better that they have the same default to avoid this kind of confusion?

I just changed it to include all variables.
Same issue: all 6 options at the bottom show the error:
Numeric condition failed to evaluate “ls: : No such file or directory…”


If the action is disabled, then it wont be evaluating the condition.

If it is, send me an export of the macro.

If you disable the action and immediately trigger the hot key, the action will still remain enabled in the engine for a short period of time.

The defaults in different actions are different. The default for the condition is disabled because they have potentially more performance needs when they are used with loops.


They are getting the error because the script is failing, giving an error/empty result, and that is not a valid number.

Post the full macro (a simplified version if necessary so other stets unrelated to this are removed), with an export. Likely there are typos in the variable name or some other issue which are very hard to debug from partial images.

Disable the action and verify that it calculates the condition even so (if it does, which I will be very very surprised at), and post that macro to. Or send it to support@stairways.com.

OMG who knows how long I've been using KM, and I had no idea that menu was there! Wow. I'd seen the carat before, of course, but just blindly assumed it collapsed the action.


1 Like

Here's the macro.

final test.kmmacros (20.6 KB)