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.
| Command | Direction | Input | Output |
|---|---|---|---|
| pbcopy | Terminal to clipboard | Standard input stream | Pasteboard contents replaced |
| pbpaste | Clipboard to Terminal | Current pasteboard | Text on standard output |
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.
| Option | Applies to | Effect |
|---|---|---|
| -pboard general | pbcopy and pbpaste | Uses the standard clipboard; this is the default |
| -pboard find | pbcopy and pbpaste | Uses the shared find pasteboard read by search fields |
| -pboard ruler or font | pbcopy and pbpaste | Uses pasteboards for text ruler and font settings |
| -Prefer txt, rtf or ps | pbpaste only | Chooses the preferred output format when several exist |
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.
| Command | What it does | Typical purpose |
|---|---|---|
| pbcopy < ~/.ssh/id_ed25519.pub | Copies a public key | Adding an SSH key to a code hosting service |
| pwd | pbcopy | Copies the current directory path | Pasting a path into a file dialog |
| pbpaste | sort | uniq | pbcopy | Sorts and deduplicates clipboard lines | Cleaning a pasted list in place |
| git diff | pbcopy | Copies a code diff | Sharing changes in a review discussion |
| pbpaste > notes.txt | Saves the clipboard to a file | Keeping copied text for later |
| printf %s token | pbcopy | Copies text without a newline | Pasting values into password-style fields |
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.
| Platform | Copy command | Paste command | Notes |
|---|---|---|---|
| macOS | pbcopy | pbpaste | Built in, no dependencies |
| Linux with X11 | xclip or xsel | xclip -o or xsel -o | Usually installed separately |
| Linux with Wayland | wl-copy | wl-paste | Provided by the wl-clipboard package |
| Windows | clip.exe | Get-Clipboard in PowerShell | clip.exe copies only |
| Remote terminal sessions | OSC 52 escape sequence | Depends on the terminal app | Works through SSH when supported |
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.