pbcopy and pbpaste: The Mac Clipboard in Terminal
macOS gives Terminal a direct line to the system clipboard — the "pasteboard," hence the pb. pbcopy puts text on it; pbpaste pulls text off it. Once those two commands click, you stop selecting Terminal output with the mouse forever.
pbcopy: send output to the clipboard
pbcopy takes whatever you pipe into it and makes it the clipboard's contents, ready to paste anywhere with Cmd-V:
# copy a file's contents to the clipboard
pbcopy < ~/.zshrc
# copy the output of any command
ls -la | pbcopy
# copy the current folder's path
pwd | pbcopy
This is the clean way to share a config file, an error log, or a directory listing: no dragging a selection across the Terminal window, no missing the first line. Whatever was on the clipboard before is replaced — the pasteboard holds one item at a time, so if you need the old contents, paste them somewhere first.
The single most common real-world use is copying an SSH public key. Run pbcopy < ~/.ssh/id_ed25519.pub and the key lands on the clipboard complete and exact, with no risk of the selection missing a character at either end — exactly what you want before pasting it into GitHub or a server control panel, where one dropped character means a confusing authentication failure later.
One caveat: most command output ends with a newline, and pbcopy faithfully keeps it. Paste the result of pwd | pbcopy into a search field or chat box and that trailing return can submit the form before you're ready. When it matters, strip it first with pwd | tr -d '\n' | pbcopy.
pbpaste: pull the clipboard back out
pbpaste is the mirror image: it prints the current clipboard to standard output, which means you can redirect it into a file or feed it to another command:
# save the clipboard to a file
pbpaste > snippet.txt
# search the clipboard for something
pbpaste | grep http
# count words in whatever you just copied
pbpaste | wc -w
Copy a wall of text from a webpage, then pbpaste | grep to fish out just the lines you need — often faster than pasting into an editor and searching there.
pbpaste is also the honest way to inspect what's really on the clipboard. Clipboard managers and password tools sometimes transform what you copied, and invisible characters survive a normal paste unnoticed; printing the contents in Terminal shows the exact bytes, stray whitespace and all. If a "correct" password keeps failing after a copy and paste, this is how you find the trailing space that's sabotaging it.
One-liners worth stealing
The real power move is using the clipboard as both input and output — copy messy text, run it through a pipeline, and the cleaned-up version is on your clipboard before you switch apps:
# sort clipboard lines and remove duplicates
pbpaste | sort -u | pbcopy
# strip rich-text formatting from copied text
pbpaste | pbcopy
# number the lines of whatever you copied
pbpaste | nl | pbcopy
That middle one deserves a note: pbpaste emits plain text by default, so a bare round trip through the two commands strips fonts, colors, and links from styled text you copied out of a browser or Word. It's a Terminal-flavored "paste and match style." Both commands deal in plain text only — you can't pbcopy an image or a file icon, just its textual form.
Worth knowing: the pasteboard these commands touch is the same one Universal Clipboard shares between devices. Text you pbcopy on your Mac can be pasted on your iPhone or iPad a moment later, provided both devices are signed into the same Apple ID with Handoff turned on — a surprisingly quick way to move a link or a snippet of code to your phone without messaging yourself.
Formatting problems solved at the source
If the reason you're stripping formatting is that Cmd-V keeps pasting fonts and colors you don't want, macOS has a built-in answer too: Option-Shift-Cmd-V pastes and matches the destination's style in most apps. Our guide to pasting without formatting on Mac covers that shortcut and how to make plain pasting the default per app.
While you're picking up Terminal tricks, Mainspring turns 90+ hidden macOS settings into labelled, reversible toggles — the tweaks without the pipelines, and every one has an off switch.
Try Mainspring free →Signed & notarized by Apple · 1-day free trial · $29 once