Shell Script Using KM Variable Problem

I'm trying to use KM variables in Shell script. See my screenshot below. I have added comments to show what I mean.
"echo" the variable returns the correct string (Green), but when I use it in "jq" command, the return result is not what I wanted (Red as opposed to Yellow).
What did I do wrong and how can I put it in the right way?

Edit: I think I know why it returns 4 instead of 2 names. It is due to the double quotation mark. If I put ".id=='27447',.id=='5439'" in double quotation mark, it will return 4 names (I still know why though).

The problem is, if I delete the quotation mark, the script will return error, and KM gives an error notification below:


Specifically, the double quoted string with $KMVAR_VarTest is itself within a single quoted string, and within single quoted strings, variables are not interpolated by the shell. So everything within the single quoted string is passed to jq as a parameter verbatim, and since it does not process environment variables, it sees the plain text $KMVAR_VarTest as part of it, not whatever is in the VarTest variable.


cat | jq -r '.[] | select("'"$KMVAR_VarTest"'") | .name'

It might be treated as:

cat | jq -r '.[] | select(".id=='27447',.id=='5439'") | .name'

But still, the double quote poses a problem. the return result is still all names instead of the two wanted names.
I guess this is a Shell Script issue, beyond what Keyboard Maestro can do. Fortunately, I have a workaround.

In your case, you do not want any quotes in the select statement, so you do not want any double quotes within the single quotes.

cat | jq -r '.[] | select('"$KMVAR_VarTest"') | .name'

Tried this. Doesn't work.

cat | jq -r '.[] | select('"$KMVAR_VarTest"') | .name'

There is an error message:

Lose the single quotes from the variable.

Looking closely at the one that works, you have:

cat | jq -r '.[] | select(.id=='27447',.id=='5439') | .name'

The parameter to jq is actually"

.[] | select(.id==27447,.id==5439) | .name

It's actually the concatenation of:

'.[] | select(.id=='
') | .name'

With all the single quotes removed. So your use of single quotes in VarTest is completely incorrect - the first single quote (before 27477) is actually matching up with the single quote before the . in the parameter.

Your working one could be simplified:

cat | jq -r '.[] | select(.id==27447,.id==5439) | .name'

which would probably make your life easier.


Thank you so much!
I have to confess that I had almost no programming background. I started learning Python about two weeks ago. Now I'm using Shell script, AppleScript, HTML, JavaScript, RegEx etc. in Keyboard Maestro. Of course, I mostly google for examples or copy codes from elsewhere and revise them to suit my need.

The experience has been amazing. But I could stuck at a very simple issue.
I did not fully understand from the wiki page example that "'"..."'" would become "..."; therefore, if I don't want the double quote, I could do '"..."' -_-!!
Neither did I know that I could remove the single quotes after ".id==". (I just followed the API example provided by a website. )

Thank you so much for your patience and detailed explanation!

1 Like

No worries, quoting is always a challenging subject because you have multiple levels each interpreting the quotes differently.

Even with all my experience I didn't notice the odd quoting in your first original working (yellow) action.

1 Like