How can I bind a bash alias to a keystroke in KM7

How do I bind a bash alias to a keystroke in KM7 ?

Aliases are only for interactive shells & KM only runs a non-interactive shell.

This is not strictly true. Keyboard Maestro runs a non-interactive shell, so bash will not read the same initialization files as when run as an interactive shell.

However bash aliases can still be used in non-interactive scripts, but you will have to create them in your script or read them in from some file explicitly.

What are you actually trying to do?

This question emerged as I tried to create a Keyboard macro to open the most recent screenshot from desktop (asked as a different question previously and was resolved).

My ~/.bash_profile had this alias

alias sso='cd ~/Desktop; rename -v "s/Screen Shot (\d\d\d\d-\d\d-\d\d) at (\d{1,2}\.\d\d\.\d\d) ([AP]M)\.png/ScrShot_$1_$2_$3\.png/" *.png; open "$(ls | grep png | sort -r | head -1)"'

It would open the screen shot from the terminal by using this alias. So Initially thought I could call this from the execute shellscript step of Keyboard Maestro. But when it didn't work as I thought it would, after some re-reading, figured aliases are only for interactive shells or else I have to use shopt to expand aliases in scripts.

So then, how can I bind the above alias to a KM7 keyboard shortcut ?

Just include the alias in your script in Keyboard Maestro, for example

Alternatively, put the aliases you want to use in both Terminal and Keyboard Maestro in a file and source that file in your .bash_profile and your Keyboard Maestro action.

1 Like

Wow. That's so simple and cool.

Hey @macrolide,

What's the point of doing this?

Unless the command sequence will change, and you're using your Bash Profile as a database (or have another good reason) it makes more sense to run the bare command from Keyboard Maestro.

It can be done as Peter has pointed out above, but then again what would be the point? Especially when you're declaring the alias in the same script space you're running it from.

Better to go this route I think:


You do know that rename does not exist on a stock macOS system – yes?

Homebrew has it though.


The point is

I am using KM for automating my workflow. And I want my keyboard to be mapped to perform different things in different contexts, say F7 will execute one thing in the context of "Finder" and another thing in the context of another app. So rather than switching to the terminal and typing the whole command, when working on say photoshop, I'd rather let KM do the opening of the image in the context of photoshop..

Sure 'rename' does not exist in stock Mac OS, but if its installed, we can still call it inside scripts using the full path can't we ?

Hey @macrolide,

This makes no sense in terms of why you need to use a bash alias in Keyboard Maestro.

You can simply run the command from an Execute a Shell Script action with whatever trigger keystroke you want (either global or app-specific).


Although I'd much rather set an environment variable in Keyboard Maestro's variables panel:

Variable name:


My PATH in Keyboard Maestro.


** If's a bit different than what I have in my Bash Profile.

If I didn't use the ENV_PATH for some reason I'd export the PATH at the top of ever relevant script:

export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;


Hey @ccstone

If you had read my first reply to @peternlewis, (perhaps) the question would have made sense. So if you still don't understand the point please the thread from top down without skipping a post.. Here it is for your kind review

The current question was resolved by @peternlewis.

But still thank you for elaborating the "best practice" using environment variables.


Well, now. That's quite rude.

I have in fact read every post in the thread from the top, and I was writing my post before Peter's showed up on the forum.

In general it makes no sense to do this:

alias longls='ls -l'
longls ~

When you can do this:

ls -l ~

Unless of course you plan to reuse the command in the same script more than once, or for the reasons I described in previous posts.


Oh is it rude ? That's really sad.
Because I felt it was quite rude and confrontational on your side to throw at someone with the usual stack-overflow forum debugging statement -- "it makes no sense doing this / why do you want to do this ?" , rather than try to be a little more constructive explaining why you think the question is inadequate or try to give an answer the best you can without a confrontational or belittling tone ?

It doesn't matter you can do ls -l ~. That is not the point of the question. . ( Can't you do alias ll=ls -lha instead of alias longls=ls -l to save 3-4 keystrokes ? ) If you had read the post properly, you would have noticed that I did not suggest creating the alias longls either. Thus it looks like you didn't [bother to] understand the question at all.

See The point of the question is not to write the shortest bash command or how to do shell scripting.
Hence was telling you to read the entire post.

Unless of course, you believe only people with your level of expertise in shell commands should be asking questions about an execute script step in KM, which then is quite rude on your side - yes ?

Closed since this topic was degenerating. Please avoid personal attacks. Thanks.

Hey @macrolide,

I'm sorry you felt my β€œWhat's the point?” question was confrontational.

It was not intended to be – it was a genuine question. I was not being condescending.

I have read the entire thread more than once, and I still think the point is muddy.

If I don't understand the needs/wants of a user – I can't respond properly – so I was trying to clarify things.

Best Regards,