How to Pass The Clipboard Into a Bash Script

I want to do something like this :

But I am not sure how to paste system clipboard in the shell command there.

Thank you for any help.

I tried this :

cd ~/play/ git clone $KMVAR_[%CurrentClipboard%]

But that doesn’t work either. :frowning:

You can’t use KM token for Clipboard in Shell Scripts.

See pbcopy & pbpaste: Manipulating the Clipboard from the Command Line

Here’s an example from @peternlewis:

In this case, you probably just want to first put the clipboard into a variable and then use the variable in the script:

Set Variable “Clip” to Text %CurrentClipboard%
Execute Script: git clone “$KMVAR_Clip”

Thank you guys.

This works :

Peter, I’m a real novice bash user. Why is it better to do that rather than use pbpaste directly in the bash script?

Wrote a small article about how I use this macro. :slight_smile:

No particular reason, just less script and more actions. I’m not much of a bash scripter, so @nikivi’s a=$(pbpaste) is new to me, but that is a nice little trick.

How can I modify this macro :

clone to play.kmmacros (25.2 KB)

So that the notification only plays when the git clone actually completes.

Thank you for any help.

I don't know, maybe change to "synchronously" ?

There is no option for that :

then just try “ignore results”.

I think that it executes synchronously by default with the other options.

To easily pass in a variable to a shell script, I figured out that you can use the With input from feature by choosing the Variable option, and then typing in your shell script as:

xargs <your script that expects a variable at the end>

For example, if your variable is called text, your shell script to echo the value of text would be:

<First set "With input from" "Variable" to "text">
xargs echo

Hey Daks,

Welcome to the forum!   :smile:

This thread was misnamed, and I've changed it to reflect the use of the Clipboard.

Handling variables is well understood and documented:

Shell Scripting

pbpaste is no doubt the most efficient method to use for this particular task.

xargs is potentially dangerous for those who are not familiar with it.

Here's a method that is more straightforward:

Use the Clipboard in a Shell Script v1.00.kmmacros (5.4 KB)
Keyboard Maestro Export

This method also works when using a variable for the input value.

And of course you can directly read Keyboard Maestro variables using the form $KMVAR_VariableName

Note that you cannot write Keyboard Maestro variables from the shell.

-Chris