Pipe Text to and From the Clipboard With pbcopy and pbpaste
pbcopy puts whatever you pipe into it on the Mac clipboard, and pbpaste writes the clipboard back out to the terminal or a file. Together they let you move text between files, commands and apps without opening an editor or selecting anything with the mouse.
Below: the basics, copying a file or command output, pasting into a file, cleaning up text in a pipeline, and the plain-text-versus-rich-text details that trip people up.
What pbcopy and pbpaste do
According to the pbcopy man page, pbcopy takes standard input and places it on the pasteboard, and pbpaste writes the pasteboard to standard output. "Pasteboard" is the older NeXTSTEP name for the clipboard, which is where the pb comes from.
If you don't name a pasteboard, both commands use the general one, the same clipboard that Cmd-C and Cmd-V use. So text you copy in any app is available to pbpaste, and text you send to pbcopy can be pasted into any app.
Both ship with macOS. There's nothing to install.
Copy a file or command output to the clipboard
Send a file
To put the text of a file on the clipboard:
cat ideas.txt | pbcopyScripting OS X points out that this is quicker than opening the file, selecting all and copying. It works well after you've been counting words with wc and want to paste the result somewhere.
Send command output
Anything a command prints can go to the clipboard the same way:
uuidgen | pbcopy
ipconfig getifaddr en0 | pbcopyThe first generates a UUID and copies it. The second copies your Mac's IP address on the en0 interface (usually Wi-Fi on a laptop).
Check what you copied
pbcopy prints nothing, so you can't see what landed on the clipboard. Run pbpaste to find out:
uuidgen | pbcopy; pbpastePaste the clipboard into a file
Redirect pbpaste the way you would any command:
pbpaste > notes.txt
pbpaste >> notes.txtThe first line writes the clipboard to notes.txt. The second appends it to the end, which is handy for building a running file from snippets you copy one at a time, as SS64's pbpaste page shows. Be careful with a single >: it replaces the file's contents without asking.
Clean up clipboard text in a pipeline
The pattern worth learning is read, change, write back:
pbpaste | sort | uniq | pbcopyThat takes a copied list, sorts it, removes adjacent duplicates, and puts the result back on the clipboard. If you haven't used the two commands before, see sort and uniq for how they behave.
Two more examples from SS64:
pbpaste | sed 's/ /%20/g' | pbcopy
pbpaste | base64 | pbcopyThe first replaces spaces with %20. The second replaces the clipboard with a base64-encoded version of itself. Any command that reads standard input and writes standard output can sit in the middle.
Plain text, rich text and encoding
The man page says pbpaste looks first for plain text, then Encapsulated PostScript, then rich text. If none of those is on the pasteboard, for example because you copied an image, it prints nothing. That's the usual reason for an empty result.
To change the order, use -Prefer with txt, rtf or ps:
pbpaste -Prefer rtfThe man page lists one limit: there's no way to ask pbpaste for only one data type. -Prefer changes what it tries first, not what it's allowed to return.
Going the other way, pbcopy stores input as plain text unless it starts with an RTF or EPS header, in which case it's stored as that type.
Both commands use your locale settings to choose an encoding. The man page recommends UTF-8, and Terminal sets the locale for you, so this mostly matters in scripts and remote sessions where LANG may be unset.
Common problems and fixes
The clipboard is empty or stale. If pbpaste shows old text, the last thing you copied may not have been text. Copy the text again, then run pbpaste | head to confirm what's there before you rely on it.
Extra blank line at the end. Commands like echo add a trailing newline, and it travels to the clipboard with the text. If that gets in the way, printf 'text' | pbcopy copies it without the newline.
Accented or non-Latin characters look wrong. That points to the encoding issue above. Check that LANG is set to a UTF-8 value, for example LANG=en_US.UTF-8, before you run the command.
The command isn't found. pbcopy and pbpaste exist only on macOS. Over SSH to a Linux machine they won't be there, and tools such as xclip or wl-copy take their place.
Be careful pasting commands
The clipboard is also how most people run commands they find online. Apple's support page on Terminal paste blocks says macOS may show a "Paste blocked" alert if you copied a command from a website, chat or messaging app. Apple's advice is not to paste a command unless you're certain what it does and where it came from.
The same goes for pbpaste. Look at what's on the clipboard before you pipe it into sh or anything else that executes it.
Doing this with Carets
The clipboard is a handy hand-off, but text you care about belongs in a file you control. Carets is a native notes and code editor for iPhone, iPad and Mac that keeps everything as plain text, Markdown and code, organized into files, projects and tags. A command output you saved with pbpaste >> notes.txt opens as an ordinary text file, with syntax highlighting if it's code or config.
Because the files stay plain text, they work with the same pipes and redirects you just used, and nothing is locked into a format.
Get Carets for iPhone, iPad and Mac on the App Store.
Frequently Asked Questions
Does pbpaste remove the text from the clipboard?
In everyday use, no. pbpaste writes the clipboard to standard output and the text stays available to paste elsewhere. The man page's wording about "removing" data is old, so if it matters, test it on your Mac with echo hi | pbcopy; pbpaste; pbpaste.
Why does pbpaste print nothing?
The clipboard probably holds something that isn't plain text, EPS or rich text, such as an image. The man page says pbpaste produces no output when none of those types is present.
Can pbcopy copy rich text?
Only if the input starts with an RTF or EPS header. Otherwise pbcopy stores what you pipe in as plain text.