Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe to/from the clipboard in a Bash script

Is it possible to pipe to/from the clipboard in Bash?

Whether it is piping to/from a device handle or using an auxiliary application, I can't find anything.

For example, if /dev/clip was a device linking to the clipboard we could do:

cat /dev/clip        # Dump the contents of the clipboard cat foo > /dev/clip  # Dump the contents of "foo" into the clipboard 
like image 935
moinudin Avatar asked Apr 14 '09 22:04

moinudin


People also ask

How do I paste from clipboard to bash?

You can now press Ctrl+Shift+C to copy selected text in the Bash shell, and Ctrl+Shift+V to paste from your clipboard into the shell. Because this feature uses the standard operating system clipboard, you can copy and paste to and from other Windows desktop applications.

How do I bash a pipe to a file?

To use bash redirection, you run a command, specify the > or >> operator, and then provide the path of a file you want the output redirected to. > redirects the output of a command to a file, replacing the existing contents of the file.

How do I copy Linux output to clipboard?

To copy text into a clipboard, you need to use a command like $ echo "Hello World" | xclip - i . It stores the text into a clipboard. To paste the output, we need to use the $ xclip - o command, and it will copy the text from the clipboard and paste out the stored output Hello World on the screen.

What is the pipe command in bash?

A pipe in Bash takes the standard output of one process and passes it as standard input into another process. Bash scripts support positional arguments that can be passed in at the command line. Guiding principle #1: Commands executed in Bash receive their standard input from the process that starts them.


2 Answers

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.

like image 68
lhunath Avatar answered Sep 29 '22 04:09

lhunath


Make sure you are using alias xclip="xclip -selection c" or else you won't be able to paste using Ctrl+v.

Example: After running echo -n test | xclip, Ctrl+v will paste test

like image 44
doug Avatar answered Sep 29 '22 04:09

doug