While mucking about in bash with some files containing rather cryptic text, I needed to easily copy and paste it into a web browser. I had hoped that perhaps there was a built-in tool or interface that could help me. Perhaps /dev/clipboard? Yes, I’m that naïve.
The crux of the matter is that I’m using the X Window system to present Gnome to me. Bash needs to pass information up to X and to do that you’ll need a spiffy little package called xclip. It’s not standard in my distro and will likely not be in yours, so you’ll need to consult your repositories.
It turns out that there’s several different clipboard-like interfaces for X and I will not pretend to understand each of them. Simply saying that you’re going to put something into X’s clipboard isn’t specific enough. xclip can redirect bash output to various X displays (it defaults to $DISPLAY if no display is explicitly stated) and to the sundry X selections (primary, secondary or clipboard).
What would a more common usage of xclip be? Perhaps:
cat ~/.ssh/id_dsa.pub | xclip -selection CLIPBOARD
Now you can paste your hairy public keys somewhere useful and not have to worry if you captured any bad characters or not. Or perhaps puke your .vimrc file to pastebin for bragging rights. Do you have any other ways to pipe terminal output to X? Let me know in the comments.