Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Execute small bits from the clipboard in Terminal UNIX
Perl and Bash programmers will recognize the backtick operator (`some_command`) as a way to cause the string enclosed in the backticks operator (some_command) to be executed as a command in the shell.

If you put some executable text on the OS X clipboard, this can also be directly executed with the backtick operator. For example, the shell command ls -l -G -a -F $HOME produces a colorized long listing of the user's home directory. Copy that command to the OS X clipboard.

In Terminal, type pbpaste, and you will get the text of the command. If you instead type `pbpaste`, the shell will execute the command and produce the directory listing.

[robg adds: This tidbit is most useful as part of a larger script, as seen in these examples. If you're in Terminal with some executable Unix command on the clipboard, you could run that command directly by pressing Command-V and Return.]
    •    
  • Currently 2.42 / 5
  You rated: 3 / 5 (19 votes cast)
 
[8,855 views]  

Execute small bits from the clipboard in Terminal | 10 comments | Create New Account
Click here to return to the 'Execute small bits from the clipboard in Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Execute small bits from the clipboard in Terminal
Authored by: mario_grgic on Mar 25, '10 07:38:33AM

Someone should also mention that this is a security concern as well esp. when done in the script, since even if you put something on the pasteboard and immediately executed it in the next line, there is a possibility that another application will overwrite or modify your command string.

So, better check what is in the pasteboard before you blindly execute it. Perhaps you put "sudo rm -rf /" in there some time ago :D.



[ Reply to This | # ]
DANGER STOP NO
Authored by: Anonymous on Mar 25, '10 11:26:35AM
Thank you for pointing this out. It's too obvious that ANYTHING can monkey with the pasteboard. You might have copied some innocent text elsewhere that causes a whole trainwreck of grief at the command line.

[ Reply to This | # ]
Execute small bits from the clipboard in Terminal
Authored by: asmeurer on Mar 25, '10 07:17:28PM

Good point. I would recommend against using `pbpaste` in any script for this reason.



[ Reply to This | # ]
Execute small bits from the clipboard in Terminal
Authored by: mankoff on Mar 25, '10 07:52:08AM

And the reverse works and is useful too... pbcopy takes output from the terminal into the clipboard.

So:

`pbpaste` | pbcopy

Will put that listing of files into the clipboard.

---
http://kenmankoff.com



[ Reply to This | # ]
Interesting result...
Authored by: leamanc on Mar 25, '10 08:01:38AM

I'm on Snow Leopard with all the latest updates. Copying the above command to the clipboard and using `pbpaste` as described in the hint, I get the following error:


ls: $HOME: No such file or directory

But by pasting the command directly (with command-V), the $HOME shell variable is parsed properly, and I get the expected ls output.

Edited on Mar 25, '10 08:02:32AM by leamanc


[ Reply to This | # ]
Me too, needs eval
Authored by: lullabud on Mar 25, '10 11:20:24AM
dho@dt:~$ echo 'ls -l -G -a -F $HOME' | pbcopy
dho@dt:~$ pbpaste
ls -l -G -a -F $HOME
dho@dt:~$ `pbpaste`
ls: $HOME: No such file or directory
dho@dt:~$ eval `pbpaste` | head -n 3
total 232
drwxr-xr-x@ 56 dho staff 1904 Mar 25 11:16 ./
drwxr-xr-x 5 root admin 170 Feb 19 09:21 ../


[ Reply to This | # ]
Execute small bits from the clipboard in Terminal
Authored by: vashremix on Mar 25, '10 08:31:15AM

I receive the same error 'ls: $HOME: No such file or directory' when I run the `pbpaste` command in the terminal in 10.5.8 (G5)



[ Reply to This | # ]
Execute small bits from the clipboard in Terminal
Authored by: S Barman on Mar 25, '10 01:34:55PM
Use eval in front of the command to have the shell "evaluate" the command and perform the substitutions.
eval $(pbpaste)


[ Reply to This | # ]
Execute small bits from the clipboard in Terminal
Authored by: asmeurer on Mar 25, '10 07:20:35PM

I think one way that this could be useful is if you copied something that you want to include as part of a larger command, but you think there may be a newline at the end of the clipboard text. If you try pasting it in to the Terminal, it will execute the command prematurely because of the newline.

It also might be useful if your readline support isn't that great and pasting multiple commands separated by newlines at once doesn't work out so well. `pbpaste` should do the trick much better.



[ Reply to This | # ]
Execute small bits from the clipboard in Terminal
Authored by: nine_mirrors on Mar 26, '10 03:03:00AM

Using back ticks for command substitution has been superseded by the '$(...)' form.
It's also convenient for people with non-us keyboards (like me) since the back tick key is impractical.
Replacing back tick with $() gets you

[erik@supernova dev]$ eval $(pbpaste)
drwxr-xr-x 2 erik staff 68 11 Jan 12:45 glassfishv3/
drwxr-xr-x 4 erik staff 136 5 Feb 15:13 java7/
drwxr-xr-x 8 erik staff 272 18 Feb 11:50 terracotta/
[erik@supernova dev]$




[ Reply to This | # ]