How to pbcopy

Content on WhatAnswers is provided "as is" for informational purposes. While we strive for accuracy, we make no guarantees. Content is AI-assisted and should not be used as professional advice.

Last updated: April 4, 2026

Quick Answer: The `pbcopy` command in macOS is a utility that allows you to pipe standard input into the system's clipboard. You can use it by piping the output of another command or by redirecting a file's content into it.

Key Facts

What is pbcopy?

pbcopy is a command-line utility found on macOS operating systems. Its primary function is to take input from standard input (stdin) and place it directly onto the system's clipboard. This makes it incredibly useful for developers, system administrators, and anyone who frequently works with the command line and needs to transfer text snippets to graphical applications or other command-line processes.

How to Use pbcopy

The most common way to use pbcopy is by piping the output of another command into it. This means you can capture the results of a command and have them immediately available in your clipboard, ready to be pasted elsewhere.

Basic Usage with `echo`

Let's say you want to copy a simple string like "Hello, World!" to your clipboard. You can do this with the echo command:

echo "Hello, World!" | pbcopy

After running this command, the text "Hello, World!" will be in your clipboard. You can then switch to any application (like a text editor, email client, or even a web browser) and paste the content using Command+V.

Copying Command Output

This is where pbcopy becomes particularly powerful. Imagine you've run a command that produced a long list of results, and you want to paste those results into a document or share them with a colleague. Instead of manually selecting and copying the text from your terminal window (which can be cumbersome), you can pipe the output directly:

ls -l | pbcopy

This command will list the contents of the current directory in a long format and copy that entire list to your clipboard.

Copying File Contents

You can also use pbcopy to copy the entire content of a text file to your clipboard. This is achieved by redirecting the file's content to the standard input of pbcopy using the cat command:

cat my_document.txt | pbcopy

This will read the entire content of my_document.txt and place it onto your clipboard.

Using `pbcopy` with Other Tools

pbcopy integrates seamlessly with many other command-line tools. For example, if you use grep to find specific lines in a file and want to copy those lines:

grep "important keyword" logfile.txt | pbcopy

This will find all lines containing "important keyword" in logfile.txt and copy them to your clipboard.

The Counterpart: `pbpaste`

Just as pbcopy copies text to the clipboard, pbpaste retrieves text from the clipboard and sends it to standard output. This is useful if you want to paste clipboard content back into a file or use it as input for another command.

pbpaste > pasted_content.txt

This command will take whatever is currently on your clipboard and save it into a file named pasted_content.txt.

Common Use Cases

In summary, pbcopy is an essential tool for macOS users who leverage the command line, providing a direct bridge between terminal output and the system clipboard.

Sources

  1. pbcopy - Copy to Clipboard - macOSfair-use
  2. pbcopy(1) Mac OS X Manual Pagefair-use

Missing an answer?

Suggest a question and we'll generate an answer for it.