Pipe to/from the clipboard in a Bash script

There are a wealth of clipboards you could be dealing with. I expect you’re probably a Linux user who wants to put stuff in the X Windows primary clipboard. Usually, the clipboard you want to talk to has a utility that lets you talk to it.

In the case of X, there’s xclip (and others). xclip -selection c will send data to the clipboard that works with Ctrl + C, Ctrl + V in most applications.

If you’re on Mac OS X, there’s pbcopy. E.g., cat example.txt | pbcopy

If you’re in Linux terminal mode (no X) then look into gpm or Screen which has a clipboard. Try the Screen command readreg.

Under Windows 10+ or Cygwin, use /dev/clipboard or clip.

Leave a Comment