The Terminal to GUI Gap
If you are working in the macOS Terminal and you generate an SSH public key (a massive block of random text), you usually need to copy that key and paste it into a web browser (like GitHub or AWS).
The traditional method is to use cat ~/.ssh/id_rsa.pub to print the key to the terminal screen, highlight the massive block of text with your mouse, right-click, and select “Copy.” This is highly prone to error. You might accidentally miss the last character or copy a blank space, which will instantly break the cryptographic authentication.
Apple solved this problem by building a bridge between the command-line interface and the macOS graphical clipboard (the Pasteboard). The commands are pbcopy (Pasteboard Copy) and pbpaste (Pasteboard Paste).
1. Using pbcopy
The pbcopy command takes any standard output in the terminal and shoves it directly into your Mac’s clipboard (Command+C), bypassing the screen entirely.
To perfectly copy your SSH public key without using your mouse:
cat ~/.ssh/id_rsa.pub | pbcopy
Nothing will print to the screen. However, if you open a web browser or a text editor and press Command+V, the exact, flawless text of your SSH key will be pasted.
Copying the Output of Commands
You can pipe the output of any command into pbcopy.
If you want to instantly copy a list of all running processes to send to the IT helpdesk:
top -l 1 | pbcopy
If you need to copy the exact path of the directory you are currently sitting in:
pwd | pbcopy
2. Using pbpaste
The pbpaste command works in reverse. It takes whatever is currently in your Mac’s clipboard (something you highlighted on a webpage and pressed Command+C to copy) and drops it directly into the terminal.
Suppose you copied a complex URL from a web browser, and you want to use the curl command to download it.
Instead of manually typing the URL, you can wrap the pbpaste command in backticks or a subshell so the terminal executes it as part of the command:
curl -O $(pbpaste)
The terminal will instantly download the file located at the URL in your clipboard.
Saving Clipboard Contents to a File
If you copied a massive block of JSON text from a Slack message and you want to save it as a physical file on your Desktop, you can redirect the output of pbpaste directly into a file.
pbpaste > ~/Desktop/data.json
This creates a perfectly clean file containing exactly what was in your clipboard.
Conclusion
The pbcopy and pbpaste commands are essential workflow enhancements for any macOS power user or developer. By eliminating the need for manual mouse highlighting, they bridge the gap between the UNIX backend and the graphical interface, ensuring fast, error-free data transfer.