Piping Bash Output to the X Clipboard

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.


  1. Kyle Brandt

    November 28, 2011 at 2:51 pm

    A couple other things to keep in mind with xclip the Linux clipboard:

    1. There are two different buffers, one for the text you have highlighted and can paste with the middle mouse button and another for Ctrl-C and Ctrl-V. These are respectively the primary and secondary you mentioned.

    2. You can also pipe the output of the clipboard(s) to other CLI programs using `xclip -o `. I tend to use this pretty often to quickly format something with sed, awk, or perl.


    • Wesley David

      November 28, 2011 at 5:57 pm

      Yes, I believe the dual clipboard was causing me some consternation that evening as I was using xclip. I know I just need to dive into the topic of which parts of which environment use which clipboard, but until then I’m going to be in a state of perpetual annoyance over it. =)


  2. riderplus

    December 5, 2011 at 5:26 pm

    If xclipboard is involved, is there a way to print sequences of text without navigating to xclipboard’s main window? I mean…shortcuts for entries in clipboard history? (like ctrl+v+n, where n is the number of the snippet in clipboard history).


    • Wesley David

      December 5, 2011 at 5:52 pm

      That’s an interesting scenario, and sadly on that I do not know the answer to. =|


Leave a Reply

Follow TheNubbyAdmin!

follow us in feedly

Raw RSS Feed:

Contact Me!

Want to hire me as a consultant? Have a job you think I might be interested in? Drop me a line:

Contact Me!

Subscribe via Email

Your email address is handled by Google FeedBurner and never spammed!

The Nubby Archives

Circle Me on Google+!

Photos from Flickr

Me on StackExchange

The IT Crowd Strava Group

%d bloggers like this: