pbcopy

Also known as: pbpaste

On this page 7

pbcopy is a macOS command-line utility that reads standard input and places it on the pasteboard, the system clipboard used by Command-C and Command-V. Its companion pbpaste does the reverse and writes the current clipboard contents to standard output, which makes the pair a small but essential bridge between Terminal & scripting work and graphical apps.

Both tools ship with every version of macOS in /usr/bin, need no installation or configuration, and follow the Unix philosophy of doing one job inside a pipeline of commands.

How pbcopy and pbpaste work with the pasteboard

macOS stores copied data in named pasteboards managed by the pasteboard server. pbcopy connects to that service as the logged-in user, reads everything arriving on standard input until end of file, and writes it to the general pasteboard, replacing whatever was copied before.

The input is stored as plain text unless it begins with a Rich Text Format or Encapsulated PostScript header, in which case pbcopy stores it as that type. As a result, piping an RTF document into pbcopy lets it be pasted with formatting into apps such as TextEdit or Pages.

pbpaste reads the general pasteboard and prints its text representation. If the clipboard holds only an image or a file reference, pbpaste typically prints nothing, because it can output only text-based types.

CommandDirectionInputOutput
pbcopyTerminal to clipboardStandard input streamPasteboard contents replaced
pbpasteClipboard to TerminalCurrent pasteboardText on standard output
The two commands at a glance

Options and syntax of pbcopy

Both commands accept very few options, and in daily use they are almost always run without any. The options select which pasteboard is used and, for pbpaste, which data type is preferred.

OptionApplies toEffect
-pboard generalpbcopy and pbpasteUses the standard clipboard; this is the default
-pboard findpbcopy and pbpasteUses the shared find pasteboard read by search fields
-pboard ruler or fontpbcopy and pbpasteUses pasteboards for text ruler and font settings
-Prefer txt, rtf or pspbpaste onlyChooses the preferred output format when several exist
Supported options

The find pasteboard is a lesser-known feature: copying a word to it with pbcopy -pboard find makes it the search term that Command-G uses in many Cocoa apps. Text encoding follows the locale settings, so setting LANG to a UTF-8 locale prevents accented characters from turning into question marks.

Typical pbcopy patterns in Terminal

pbcopy is most valuable at the end of a pipeline, capturing the result of a command so that it can be pasted into a browser, chat or document. pbpaste is most valuable at the start of a pipeline, feeding clipboard text into text-processing tools.

CommandWhat it doesTypical purpose
pbcopy < ~/.ssh/id_ed25519.pubCopies a public keyAdding an SSH key to a code hosting service
pwd | pbcopyCopies the current directory pathPasting a path into a file dialog
pbpaste | sort | uniq | pbcopySorts and deduplicates clipboard linesCleaning a pasted list in place
git diff | pbcopyCopies a code diffSharing changes in a review discussion
pbpaste > notes.txtSaves the clipboard to a fileKeeping copied text for later
printf %s token | pbcopyCopies text without a newlinePasting values into password-style fields
Common command patterns

The last pattern addresses a frequent pitfall: echo adds a trailing newline, so pasted values may contain an invisible line break. Using printf %s or echo -n produces exactly the intended characters.

pbcopy compared with clipboard tools on other systems

Every desktop operating system has an equivalent, and cross-platform scripts must detect which one is available. The table helps when porting a script or working on mixed teams.

PlatformCopy commandPaste commandNotes
macOSpbcopypbpasteBuilt in, no dependencies
Linux with X11xclip or xselxclip -o or xsel -oUsually installed separately
Linux with Waylandwl-copywl-pasteProvided by the wl-clipboard package
Windowsclip.exeGet-Clipboard in PowerShellclip.exe copies only
Remote terminal sessionsOSC 52 escape sequenceDepends on the terminal appWorks through SSH when supported
Clipboard commands by platform

Within the macOS toolbox, pbcopy complements tools such as osascript, which can set the clipboard through AppleScript for richer data types, and open, which hands files to graphical apps.

Limitations and security considerations

pbcopy works only with text, RTF and EPS data; it cannot copy images or files as Finder would. For those tasks, scripts usually call AppleScript through osascript.

Each call replaces the entire clipboard, so pbcopy cannot append to existing content; scripts that need to accumulate text should collect it in a variable or temporary file first. Very large inputs, such as multi-gigabyte logs, are accepted but can make the receiving app slow or unresponsive when pasted, so filtering output with tools such as head or grep before copying is good practice.

In an SSH session, pbcopy runs on the remote machine. On a remote Mac it fills that computer’s clipboard, not the local one, and on headless sessions it may fail to reach the pasteboard service. Forwarding clipboard data to the local machine requires OSC 52 support in the terminal or a dedicated forwarding setup.

Copied secrets deserve caution. With Universal Clipboard, text copied on a Mac can appear on nearby Apple devices signed in to the same Apple Account, and clipboard history utilities may store passwords indefinitely. Scripts that copy tokens should clear the clipboard afterward, for example with pbcopy < /dev/null.

A non-technical view

On a Mac, the clipboard is the invisible holding place for whatever is copied with Command-C. pbcopy lets the text-based Terminal put things into that holding place, and pbpaste lets Terminal read them back out.

Think of it as a mail slot between two rooms: one room is the command line, the other is the familiar world of windows and apps. Instead of selecting long output with the mouse, a user sends it through the slot and then pastes it anywhere with Command-V.

A concrete example

A systems administrator receives a spreadsheet column of 2,400 email addresses that contains duplicates, mixed capitalization and blank lines. He copies the column with Command-C and switches to Terminal.

He runs pbpaste | tr A-Z a-z | sort -u | sed '/^$/d' | pbcopy, which reads the clipboard, converts everything to lowercase, removes duplicates and empty lines, and puts the cleaned list back on the clipboard. The command finishes in well under a second.

He then pastes the result into the mailing system, which now accepts 1,870 unique addresses without errors. The whole cleanup takes about a minute, without saving any intermediate file and without manual editing.