Notes

Copy contents to clipboard form command line (Ubuntu, macOS)

copying file contents to the clipboard from the terminal with pbcopy on macos and xclip on ubuntu, and why xclip fails without X11

Edit on GitHub

Commands

macOS

macOS comes with pbcopy

1pbcopy < ~/.ssh/id_rsa.pub
2
3# or 
4
5cat ~/.ssh/id_rsa.pub | pbcopy

Ubuntu

On Ubuntu you can use xclip

1sudo apt install xclip
2xclip -selection clipboard -i < inputFile
3
4# xclip -sel c < inputFile
  • clipboard is a parameter for -selection
  • -i means inoput (standard input or file). Since file is default, you can omit the -i in most cases
-i, -in
      read text into X selection from standard input or files (default)

-o, -out
      print the selection to standard out (generally for piping to a file or program)

-selection
      specify which X selection to use, options are "primary" to use XA_PRIMARY (default), "secondary" for XA_SECONDARY or "clipboard" for XA_CLIPBOARD

NOTE: xclip and xsel are X11 utilities, they manipulate the X11 selections, so can only be used in a X11 environment where $DISPLAY is set. Running in a terminal only context (e.g. WSL or headless) will give you this error:

1Error: Can't open display: (null)

Related

Browse all topics