How do you copy to the clipboard from the command line?
- Mac:
command | pbcopy, andpbpasteto read it back. - Linux on X11:
command | xclip -selection clipboard, and add-oto paste. - Linux on Wayland:
command | wl-copy, andwl-pasteto paste. - Windows:
command | clipin Command Prompt, orSet-ClipboardandGet-Clipboardin PowerShell. - WSL:
command | clip.exe, andpowershell.exe Get-Clipboardto paste. - Over SSH: an OSC 52 escape code that your local terminal turns into a copy.
This page is about moving text between commands and the clipboard. For the keys that copy and paste inside a terminal window, see copy and paste in the terminal.
| Copy | Paste | No trailing newline | |
|---|---|---|---|
| macOS | pbcopy | pbpaste | printf '%s' "$(cmd)" |
| Linux, X11 | xclip -sel c | xclip -sel c -o | xclip -r |
| Linux, X11 (xsel) | xsel -bi | xsel -bo | tr -d '\n' (one line) |
| Linux, Wayland | wl-copy | wl-paste -n | wl-copy -n |
| Command Prompt | clip | none built in | echo adds CRLF |
| PowerShell | Set-Clipboard | Get-Clipboard -Raw | adds none |
| WSL | clip.exe | powershell.exe | tr -d '\r' |
| SSH | OSC 52 | depends on terminal | strip it first |
pbcopy and pbpaste on a Mac
Both come with macOS. pbcopy reads standard input and puts it on the clipboard. pbpaste writes the clipboard to standard output. Because they are plain pipes, you can chain them.
The last line handles the newline. Most commands end their output with one, and pbcopy copies it as is. Paste a commit hash into a terminal and that newline acts like Enter. Wrapping the command in "$(...)" strips it, because command substitution drops trailing newlines. The same trick matters when you copy a file path with pwd.
The pbcopy man page says both tools pick their encoding from your locale variables, such as LANG=en_US.UTF-8. Terminal sets a UTF-8 locale by default, so accented letters come through. A cron job or a bare SSH session may have no locale set, and then non-ASCII text can get mangled. Export a UTF-8 locale in the script if you see that. Two lesser-known flags: -pboard find uses the separate Find pasteboard, and pbpaste -Prefer rtfasks for rich text when it’s there.
How do you use xclip on Linux?
X11 has more than one clipboard. The Arch Wiki lays it out. PRIMARY holds whatever you last highlighted, and a middle click pastes it. CLIPBOARD is the one Ctrl+C and Ctrl+V use. The xclip man page lists PRIMARY as the default. So echo hi | xclip runs without an error, and then Ctrl+V pastes something else. Add -selection clipboard every time.
The man page allows shortened options as long as they stay unambiguous, so -sel c means -selection clipboard. The -r flag removes one newline at the very end and leaves the rest alone.
xclip doesn’t exit right away. It forks into the background and keeps serving the text until another program takes over the clipboard. That’s how X11 works: the program that copied holds the data. Close it and the copy can disappear. A clipboard manager for Linux grabs the text so it outlives the program.
xsel and the pbcopy aliases
xsel does the same job with different flags. It also defaults to PRIMARY, says its man page, so add -b.
If your fingers already type the Mac names, two aliases in ~/.bashrc make them work on an X11 machine.
What replaces xclip on Wayland?
Ubuntu and Fedora both start a Wayland session by default. xclip and xsel speak X11, so on Wayland they only reach apps through the XWayland compatibility layer, when it’s running. The native tools are wl-copy and wl-paste from the wl-clipboardpackage. Check which session you’re in first.
The newline rules run in opposite directions. The wl-clipboard man page says wl-copy -n drops a trailing newline from the input. Separately, wl-paste adds a newline to text it prints, unless you pass -n. Like xclip, wl-copy forks into the background to serve pastes; -f keeps it in the foreground.
One oddity: on a compositor without the wlroots data-control protocol, wl-clipboard briefly opens a tiny invisible window to reach the clipboard. You may see a flicker. The man page warns it can hang if the compositor won’t focus that window.
clip, Set-Clipboard and Get-Clipboard on Windows
Command Prompt has clip. Microsoft’s page for it shows two forms and says nothing about encoding or line endings. Those are where it goes wrong, so we tested them. We ran everything in this section on 20 October 2026, on Windows 11 build 26200 with Windows PowerShell 5.1, Git Bash 5.2 and WSL 2.6.3 running Ubuntu 24.04.
echo hello| clipputhelloplus a carriage return and line feed on the clipboard. The newline from echo stays.- Text piped in with Unix line feeds came out with Windows CRLF line endings.
- UTF-8 broke. clip read the bytes using the console code page (437 on our machine). The word
cafécame out ascaf├⌐. Runningchcp 65001first fixed it. - UTF-16LE input copied cleanly. If the text starts with a byte order mark, clip keeps it as an invisible character at the start of the clipboard. It can break a pasted password or token.
In PowerShell, use Set-Clipboard. Piping to clip.exe from Windows PowerShell 5.1 sends text in ASCII by default, so café ✓ landed as caf? ?. Set-Clipboard copied the same string exactly, with no newline added.
Two more things to know. Get-Clipboard returns an array with one string per line, and -Raw returns a single string. And piping file objects straight into Set-Clipboard in 5.1 copied the files themselves, so the text clipboard came back empty. Add Out-String when you want the listing as text. Git Bash users get one more option. cat file > /dev/clipboard copied UTF-8 text correctly in our test and kept the Unix line endings.
WSL: clip.exe and powershell.exe
WSL can run Windows programs by name, with the .exe on the end, as Microsoft’s WSL docs describe. So clip.exe works from a Linux shell. It brings the same encoding bug with it: UTF-8 from Ubuntu came out garbled. Converting to UTF-16LE with iconv fixed it.
Pasting needs care too. A plain powershell.exe Get-Clipboard printed é as a single byte from code page 437, which Linux reads as junk. Setting the output encoding to UTF-8 inside the command fixed it. PowerShell ends its output with CRLF, so tr -d '\r' cleans up the carriage returns. One line feed is still left at the end.
WAYLAND_DISPLAY and DISPLAY inside WSL, and we saw both on our Ubuntu install. A script that checks for Wayland or X11 first will reach for wl-copy or xclip, which may not be installed. Check for WSL first.How do you copy to your local clipboard over SSH?
Running pbcopy or xclip on a server copies to the server, which is useless to you. The fix is an escape sequence called OSC 52. The program prints it, and your local terminal sets your local clipboard from it. xterm’s control sequence reference lists it as “Manipulate Selection Data”. The text inside travels as Base64, and your terminal has to allow it. Windows Terminal does, according to the Set-Clipboard docs, which also mention the -AsOSC52 switch added in PowerShell 7.4. tmux needs a setting before it passes the code through, and the terminal copy and paste guide covers that, plus iTerm2.
One function for every machine
These two functions pick the right tool for wherever you are. SSH comes first, so a remote shell copies to your own machine. WSL comes next, for the reason in the note above. We tested the WSL and Git Bash branches, including accented text, on the setup listed earlier. The Mac and Linux desktop branches use the commands from their man pages, and we didn’t run them on those systems.
The names avoid paste, which is already a coreutils command. Once they load, a line like curl -s $URL | jq -r '.token' | copyclip should behave the same on each of your machines. The jq cheat sheet has more filters for that middle step. If Relicruns on the desktop, everything you pipe to the clipboard also lands in its searchable history, so Monday’s output is still there on Friday.
Sources
- xclip(1) manual page, Debian Manpages
- wl-clipboard(1) manual page, Arch manual pages
- clip command reference, Microsoft Learn
- Set-Clipboard, Microsoft Learn