pbcopy & pbpaste — Use the Clipboard from the Terminal

pbcopy and pbpaste bridge the terminal and the macOS clipboard: copy command output, paste content and pipe it straight through your shell pipelines.

pbcopy and pbpaste are the bridge between the terminal and the macOS clipboard: pbcopy reads from stdin and places the content on the clipboard, while pbpaste prints its content to stdout. That lets you copy command output into graphical apps in one move, feed clipboard content into scripts, or slot both tools into the middle of a pipeline. Both commands ship with macOS, so they are available instantly with nothing to install. This guide walks you through the moves you reach for every day.

pbcopy — Copy to Clipboard

echo '<text>' | pbcopy — Copy text to the clipboard.

echo 'Hello World' | pbcopy

cat <file> | pbcopy — Copy file contents to the clipboard.

cat ~/.ssh/id_ed25519.pub | pbcopy

<command> | pbcopy — Copy command output to the clipboard.

pwd | pbcopy

pbcopy < <file> — Copy file contents using input redirection.

pbcopy < config.yaml

pbpaste — Paste from Clipboard

pbpaste — Output clipboard contents to stdout.

pbpaste

pbpaste > <file> — Save clipboard contents to a file.

pbpaste > snippet.txt

pbpaste | <command> — Pipe clipboard contents to a command.

pbpaste | wc -l

pbpaste | sort | uniq — Process clipboard text through a pipeline.

pbpaste | sort | uniq

Common Patterns

cat ~/.ssh/id_ed25519.pub | pbcopy — Copy SSH public key to clipboard.

cat ~/.ssh/id_ed25519.pub | pbcopy

git diff | pbcopy — Copy git diff to clipboard.

git diff | pbcopy

pbpaste | python3 -m json.tool | pbcopy — Pretty-print JSON from clipboard and copy back.

pbpaste | python3 -m json.tool | pbcopy

echo -n '<text>' | pbcopy — Copy text without trailing newline.

echo -n 'no newline' | pbcopy

pbpaste | grep '<pattern>' — Search clipboard contents.

pbpaste | grep 'error'

pbpaste | tr '\n' ',' — Convert clipboard lines to comma-separated.

pbpaste | tr '\n' ','

Conclusion

pbcopy and pbpaste are small helpers with a big payoff: they turn the clipboard into a first-class link in any shell pipeline and save you the chore of selecting text with the mouse. Keep in mind that pbcopy takes the data verbatim – text is stored in the encoding of the input (usually UTF-8), and echo appends a trailing newline by default, which you avoid with echo -n. Both tools exist only on macOS; on Linux, xclip, xsel or wl-copy fill the same role. If you live in the terminal, these two commands save you a surprising number of steps over the course of a day.

Further Reading

  • caffeinate – prevents the Mac from going to sleep
  • defaults – reads and writes macOS system preferences
  • diskutil – manages disks, partitions and volumes