How to Pipe to or from Clipboard in a Bash Script?

Using a clipboard is a habit of graphical user interfaces. Although it is possible to do anything you want in a text-based terminal, old habits die hard. So is there a way to use your clipboard, by either filling or emptying it, in the terminal in a FIFO?


There is a way to achieve this in a X compliant system. The program is called xclip. It is not a default program, so you need to install it before using it. SOOO, grab your favorite package manager, apt-get, yum, pacman, or dnf and install xclip.

Also, here is the source.

How to use xclip?

xclip is a more complex program to use for a person that will copy and paste only. So, using aliases will help you.

alias set_clipboard="xclip -selection c"
alias get_clipboard="xclip -selection c -o"

Also, if you will keep using xclip, you may add these aliases to either ~/.bash_aliases or ~/.bashrc .

To use those aliases:

$ echo test | set_clipboard
$ get_clipboard
> test

What if I don’t have the X?

If you’re stuck with the terminal mode, you may use another program with a clipboard. Because since you don’t have X, you don’t have a clipboard. However, mainstream programs like screen have an internal clipboard. You may try to use the screen command of readreg.


For a Mac OS X system, you may use pbcopy. It is a fairly easy-to-use program. To copy, you will just use:

cat example.txt | pbcopy

Then to paste, just use ^V!

What about Windows?

Just don’t use it. JK, if you’re up-to-date, there is a perfectly easy tool to use. The tool is named ‘clip’!

That’s all folks! See my homepage for more!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *