Copy Bash Commands to the Clipboard

There are many productivity boosters for Linux users that we probably take for granted, one being copying commands from the command line to the clipboard. Of course, a Linux Terminal Window has the ability to copy and paste with the mouse and CTRL-SHIFT-C, but this post is how to do it efficiently from the keyboard.

The traditional method of command-line clipboard copy using the mouse.

Clipboard Command Copy at the Keyboard

We all know we’re more efficient when we can stay on the keyboard, so here’s how we can perform the same copy-n-paste above at the keys.

The utility we’ll be using is xclip, a Linux Command line clipboard grabber. If you don’t have it, install it with:

$ sudo apt-get install xclip

From the screenshot above we see our sudo service apache2 restart command is #399 in our history. To copy it to the clipboard we can use

We can also use tricks like copying the previous command with

$ echo “!!” | xclip

Here’s a special bonus, a screenshot of the Diodon Clipboard Manager for Linux, an excellent desktop utility for managing your clipboard. So go ahead and copy as many commands as you like with Diodon at the ready.