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
Key Facts
- pbcopy is a command-line utility for macOS.
- It copies standard input to the system clipboard.
- Commonly used with other commands like `echo`, `cat`, or `grep`.
- Can be used to copy file contents directly.
- pbpaste is its counterpart for pasting from the clipboard.
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
- Quickly copying command outputs for documentation or sharing.
- Transferring configuration snippets between terminal sessions or applications.
- Saving specific search results from log files.
- Automating tasks where clipboard content needs to be manipulated.
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.
More How To in Daily Life
Also in Daily Life
More "How To" Questions
Trending on WhatAnswers
Browse by Topic
Browse by Question Type
Sources
- pbcopy - Copy to Clipboard - macOSfair-use
- pbcopy(1) Mac OS X Manual Pagefair-use
Missing an answer?
Suggest a question and we'll generate an answer for it.