Search code examples
linuxbashmacosclipboard

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

Solution

  • 2018 answer

    Use clipboard-cli. It works with macOS, Windows, Linux, OpenBSD, FreeBSD, and Android without any real issues.

    Install it with:

    npm install -g clipboard-cli
    

    Then you can do:

    echo foo | clipboard 
    

    If you want, you can alias to cb by putting the following in your .bashrc, .bash_profile, or .zshrc:

    alias cb=clipboard