Execute text from the OS X clipboard.

`pbpaste` | pbcopy
The backtick operator, in general, will execute the text inside the backticks. On OS X, the pbpaste command will put the contents of the OS X clipboard to STDOUT. So if you put backticks around pbpaste, the text from the OS X clipboard is executed. If you add the pipeline | pbcopy, the output from executing the command on the clipboard is placed back on the clipboard. Note: make sure the clipboard is text only.
Sample Output
abd-pb-2:~ andrew$ str="ls -l -G"        #assign a command to str
abd-pb-2:~ andrew$ echo $str             #show that it is there
ls -l -G
abd-pb-2:~ andrew$ echo $str | pbcopy    #put that command on the clipboard
abd-pb-2:~ andrew$ `pbpaste` | pbcopy    #execute the command and put output on clipboard

# command on clipboard could come from any source...


7
By: drewk
2009-09-21 23:10:11

What Others Think

May not want to copy something bad like "rm -rf ~" or something of the sort :) Could be malicious if used by someone who isn't careful. Nevertheless, +1
kaedenn · 612 weeks ago
Excellent example: man ls
drewk · 612 weeks ago
Sorry -- I was looking at the example comment instead of your comment! Right -- be careful what is on the clipboard. Same with "sudo !!" You don't get to see what is in the command history before giving it root privileges and executing!
drewk · 612 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: