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
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.
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.
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.
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.
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With