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
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
clipboardis a parameter for-selection-imeans inoput (standard input or file). Since file is default, you can omit the-iin 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
- Checking operating system in a bash script and installing different programs
- Screenshot shortcuts on macOS, Linux and Windows
- Setting up shared network between Ubuntu and Mac using Samba
- Install Ruby with rbenv and CocoaPods on M1 MacBook (Apple Silicon)
- Setting up a sudo User with SSH key access an Azure Virtual Machine (Ubuntu)