Using Shell Scripts with KM

Well, I suppose it depends on your personal experience and knowledge base.
Knowing very few Bash/Shell commands, I always look first to KM. Might take a bit of research in the KM Wiki, but at least all of the "commands" (aka "Actions") are in the Wiki and are well documented.

I have one other aversion to shell scripts: They can easily to tremendous damage to your system, either by design or accident (typo).

Your solution didn't use Keyboard Maestro any more than mine did. You bailed out to ~15 lines of JavaScript, which wasn't in the Wiki any more than Unix tools are.

Finding the answer for a shell script to do the job could be found by using the first answer in google for "reverse lines shell". There were actually about 5-6 different ways of doing it on that one page alone.

Oh come now.

That is a common fear expressed by people who don't really know the command line and who have only heard horror stories, but the likelihood of you doing anything resembling "tremendous damage" to your system by accident is infinitesimal.

There are a few commands that can do damage: rm obviously, and even rsync if given the wrong syntax. But those aren't commands that a novice is going to go anywhere near.

Could you typo rm and accidentally delete a file? Yes. Could you accidentally delete or overwrite a file with Keyboard Maestro? Yes.

You know what's far more likely? That you could want up tomorrow and find your hard drive has died. That's why we have backups.

If you are processing text -- the topic at hand -- you can find incredibly powerful tools at your fingertips via the command line. Ignoring or refusing to use such a versatile tool in your toolbox due to lack of knowledge or fear would be a shame. Passing that phobia on to others is a disservice.


I hit the heart on tjluoma's post because I believe most people who use keyboard maestro and this forum love automating things. I think using the right tool for the job is a valuable technique even though there are differences in opinion on what the right tool is. Caution should be used with different tools but backing things up and learning as much as possible will always serve somebody well.

Let's go easy on him. He's an elite wizard here. I don't want to get on his bad side, even though I sense I am already. So I'm coming to his defence. Not regarding the merit of his argument, but regarding that we shouldn't get testy with each other. He was a little hyperbolic, but he wasn't testy.

Yep, I'd plead guilty to that, although I do have a few choice Bash scripts, like find, that I really like and use fairly often.

However, I'd suggest this:

  1. Most KM users came to KM because they want to automate their workflows, and are not that familiar with any type of scripting.
  2. While Bash offers a lot, it is also hard to read for those that don't know it (including me)
  3. IMO, using a Bash script that someone else wrote, that you can't understand, can be (however unlikely) dangerous. I don't do it, and recommend the same for others)
  4. Yes, you can find the right Bash script by searching, but ONLY if you know what keywords to search for.
  5. At least with AppleScript it is somewhat readable to most.

To be clear, I am NOT anti-Bash. I just recommend being cautious about using it. :wink:

I apologize for starting this off-topic discussion. If you guys would like to continue it, I'd be happy to move it to a new thread.

1 Like

Arguably, it takes a lot less research to learn shell commands, which are all documented in the man pages. As excellent as the KM Wiki is, it doesn't come close (and not much does) to the standard of man. And, of course, man man will teach one how to use a man effectively.

Additionally, recent exports of man documentation for most of the commonly used shells are also available online.

Additionally to that, there's the rest of the internet (Stack Overflow, Superuser, etc.)

I'm by no means saying one ought to prioritise shell over native KM, or any method over any other. It's just about the right tool, for the right job, and the more tools one has at one's disposal, the bigger the scope for doing a better job.

I think, mostly, I'm extremely surprised, given your educational background, that you would be so averse to the command line. It's like a hairdresser who hates getting their hair cut. That said, some doctors can't stand needles. And, while I have a couple of mathematics degrees, I don't like numbers much at all.

That's funny... My big blunder with bash was by way of a mistyped find command (albeit using it to perform an -exec command, namely, mv, on the returned files). But if you want a function that recurses through a folder tree and takes every file and moves it either up or down the tree to any depth depending on how many vowels it has in its filename, I can pass that along.

Again, quite surprising to hear this applied to yourself. To anyone not familiar with scripting, JavaScript can appear very alien in ways that can make it appear more opaque than shell scripts.

But, this is one (of a good few) advantages FiSH shell has over bash, in that it uses syntax that is less cryptic (there's no $(...), or ${-:@}, or, or !!).

Humans, by our very nature, depend on utilising knowledge and tools created by someone who knows more than us about it, so that we can benefit from their expertise. Otherwise, you would most definitely be wanting to put your computer in the bin, stop driving your car, and avoid looking up at the sky.

I think the sentiment behind this advice/recommendation is well-intentioned, but poorly-expressed and much too general, though it certainly has merit in specific situations when applied with a mixture of common sense (which isn't actually a thing, but people seem to believe it is), and a more well-defined boundary.

The same advice would stop others being able to use your JXA scripts, or ASObjC scripts, etc.

tell application "Finder" to tell a reference to the application files in ¬
    the entire contents of (the path to the applications folder) to ¬

That's not a particularly damaging script, but it's a bloody annoying one, which I ran one time just to see what it would be like. Had to reboot. But damage can be achieved quite easily if you forget that System Events deletes things permanently, and accidentally do it with a whose filter and, say, miss out the word not or something.

Nope. Okay, you need someone to tell you there's a man command, but that's the same as needing someone to tell you there's a KM Wiki (although both the wiki and the man command are in the menu of the respective application). man -k <searchterm> is how I learned bash, although admittedly I enjoy the art of programming and learning a new language, whereas others would rather pluck out their eyelashes. But it's very doable.

Of course, I'm not saying you (or anyone) must now go and learn a shell language. It's just interesting to have heard your thoughts/preconceptions/anxieties, and it would be a shame if you were holding yourself back from doing something because of a disproportionate sense of caution (thought some caution is always advisable, as you said).


I actually intended just to post another solution to the problem, because I can't not supply an AppleScript for this:

property text item delimiters : linefeed

set input to system attribute "KMVAR_myvar" -- or one's preferred way to access a KM variable, e.g. `tell app "Keyboard Maestro Engine"...`
return the reverse of the input's paragraphs as text

Wow. I'll make a mental note for when I have math issues. CJK = Calculus, Justly Knowledgeable. I actually have a math issue I recently solved (to enhance KM's RAND function) that I'd like to post on a new thread. And I'll link your name to solicit your feedback.

P.S. Regarding this argument, I recall that plea from Rodney King, "Can we all just get along?" I'm just exaggerating here, it's not really that bad. Everyone is being quite decent.

@CJK, I'm not going to respond individually to each of your comments, but I do have a few comments in response to your post:

  1. Written like a true shell script master. :wink:
    • Unfortunately, most of us are not, especially those who are drawn to KM because it does NOT require any knowledge of, or experience in, programming/scripting.
    • Your POV fails to recognize this.
  2. Maybe you missed this comment above, but I'll repeat it here for clarity:
    • To be clear, I am NOT anti-Bash. I just recommend being cautious about using it. :wink:
    • I am NOT averse to using shell scripts; I just rarely find they are the best tool for the job.
  3. You said "It's just about the right tool, for the right job". I have been advocating that for a long time, probably before you were born. :wink:
    • It is because of that doctrine that I rarely find a use for shell scripting when writing KM Macros.
    • I find that for most automation tasks using native non-scripting KM actions work just fine.
    • When they don't, I usually find that AppleScript works very well, and it is usually the best scripting tool for the job because it is quite versatile and I can use a great IDE, Script Debugger, to develop and test my scripts. And ASObjC can do most, if not more, than Shell Scripting.
    • Finally I learned long, long, ago, that "better is the enemy of good enough".
  4. I have to strongly disagree with your assessment of KM documentation vs shell scripting documentation
    • Most KM users would not have any idea of how to get help with shell scripts, particularly in choosing the correct command to use.
    • KM has lots of built-in help right at the finger tips of the KM user
      • Every Action has a Help in the Gear menu
      • New users can show the Action menu to see a list of Actions grouped by category
      • When showing the menus for Actions, Tokens, and Functions, holding down the OPT key turns it in a direct link to the KM Wiki for that item
      • The KM Wiki has tons of help, even with many articles that provide an overview, but then with details, like Variables .

Finally I want to close with this. Regardless of your perception of my background and of your surprise of my take on shell scripting, it is not appropriate for you to make such personal statements. I am fairly thick-skinned, but your repeated use of "I think, mostly, I'm extremely surprised" grew a bit tiring and offensive. You seem to imply that I have a defect in my personality that is preventing my use of shell scripts.

Quite the contrary. Over the decades I've studied and learned many programming languages. But, by far, there are more languages that I have not studied. The ones I have studied and learned were selected because I thought at the time they were the best tools for the job. OTOH, I have learned some languages later on when I discovered their value, usually based on comments from others that I trust and respect.

So, just to satisfy your curiosity about my not choosing shell scripts, I did strongly consider doing so some time ago. And while shell scripts did fill a few voids, I did not find them worthy enough to devote an extended study to. Especially when there are other languages, like JavaScript, which are much, much more useful. Another language, much harder than shell scripting I suspect, is RegEx. While it has a steep learning curve at the beginning, it has huge rewards. I use RegEx almost daily, sometimes in KM, sometimes kn BBEdit to cleanup/restructure data.

But those were/are my choices based on my perceptions of need. I don't begrudge anyone else for choosing otherwise. To each his/her own.

@CJK, here is one of the things that makes shell scripting so hard:

Fortunately, @tjluoma provided excellent instructions just below this on what it means, and how to construct and use it. Without those instructions, I'd be totally lost. :wink:

The only fighting I want to see between two elite wizards on this forum is this one.

This is my attempt to try to lower the temperature here.