Yes, you can access the clipboard from the command line on all major operating systems. On Windows, you use clip to send text to the clipboard and powershell Get-Clipboard to retrieve it; on macOS, pbcopy and pbpaste handle both directions; on Linux, the tools vary by desktop environment, with xclip and xsel being the most common. This article explains the exact commands, how they work, and how to use them in scripts and pipelines.
What the Clipboard Is at the System Level
The clipboard is a temporary storage area managed by the operating system’s windowing system. When you copy text or data from one application, it is placed into a system-owned buffer that other applications can then paste from. The command-line tools described here are simply small programs that interface with that buffer through the system’s application programming interface (API).
On Windows, the clipboard is managed by the Windows subsystem and is accessible through the Win32 API. On macOS, it is managed by the pasteboard server (pboard). On Linux, the clipboard is typically managed by the X11 or Wayland display server, and the exact behavior depends on which one you are running.
Windows: Using clip and PowerShell
Windows provides a built-in command-line utility called clip.exe that sends the output of a command to the clipboard. To copy the contents of a file to the clipboard, you can use:
clip < file.txt
To copy the output of a command, pipe it to clip:
dir | clip
To retrieve the clipboard contents from the command line, you need PowerShell. The Get-Clipboard cmdlet returns the current text on the clipboard:
powershell -command "Get-Clipboard"
In a PowerShell session, you can also use Set-Clipboard to set the clipboard contents, which is useful for scripting:
Set-Clipboard -Value "Hello, world"
Note that clip.exe only handles text and may not preserve formatting. It also adds a trailing newline when copying a file, which can be surprising if you are scripting.
macOS: pbcopy and pbpaste
macOS includes two commands, pbcopy and pbpaste, that are simple and symmetric. To copy text to the clipboard, pipe it to pbcopy:
echo "Hello from terminal" | pbcopy
To paste the clipboard contents to standard output, use pbpaste:
pbpaste
These commands work with the general pasteboard, which is the same one used by the GUI. They also support a flag to specify a different pasteboard, such as -pboard general (the default) or -pboard find for the find bar pasteboard used in some apps.
Because pbcopy and pbpaste are standard on macOS, they are ideal for scripting. For example, you can copy the current date to the clipboard and then paste it into a document:
date | pbcopy
Linux: xclip, xsel, and Wayland Alternatives
Linux does not have a universal clipboard command because the clipboard is tied to the display server. For X11 (the traditional Linux graphical system), the most common tools are xclip and xsel. Both are usually installed via your package manager (e.g., sudo apt install xclip on Debian/Ubuntu).
Using xclip to copy:
echo "Hello" | xclip -selection clipboard
To paste:
xclip -selection clipboard -o
Using xsel to copy:
echo "Hello" | xsel --clipboard --input
To paste:
xsel --clipboard --output
Note that on Linux there are multiple selections: the clipboard (used by Ctrl+C/Ctrl+V) and the primary selection (used by middle-click paste). The commands above explicitly use the clipboard selection. If you omit -selection clipboard with xclip, it defaults to the primary selection, which can cause confusion.
If you are running a Wayland session (common on modern distributions like Fedora and Ubuntu 21.10+), xclip may not work because it relies on X11. For Wayland, you can use wl-copy and wl-paste from the wl-clipboard package. For example:
echo "Hello" | wl-copy
wl-paste
Using the Clipboard in Shell Scripts and Pipelines
One of the main reasons to access the clipboard from the command line is to integrate it into scripts. A common pattern is to copy the result of a command to the clipboard so you can paste it into an email or document. For example, on macOS, you can copy the current git branch name:
git branch --show-current | pbcopy
On Linux with xclip:
git branch --show-current | xclip -selection clipboard
You can also read the clipboard into a variable for further processing. In bash, you might do:
clipboard_content=$(pbpaste)
This is useful for scripts that need to transform clipboard content, such as stripping formatting or extracting URLs.
Another common use is to copy a file’s contents to the clipboard without opening it in an editor. For instance, to copy an SSH key to the clipboard on macOS:
pbcopy < ~/.ssh/id_rsa.pub
On Linux, the equivalent is:
xclip -selection clipboard < ~/.ssh/id_rsa.pub
Common Problems and How to Solve Them
One frequent issue is that clip on Windows adds a trailing newline when you redirect a file into it. This can break scripts that expect exact content. To avoid this, you can use PowerShell’s Set-Clipboard instead, which does not add a newline.
Another problem is that on Linux, the clipboard is lost when the application that set it closes. This is because X11’s clipboard is owned by the application that copied the content. Tools like xclip can mitigate this by forking into the background and keeping the content available, but if you are using a script that exits immediately, the clipboard may be cleared. A workaround is to use a clipboard manager like clipit or parcellite.
If you are using SSH or a remote terminal, the clipboard commands may not work because they require a local display. In that case, you might need to use terminal escape sequences or a tool like tmux to access the clipboard on the remote machine.
Finally, be aware that these commands only handle plain text. If you copy rich content (like formatted text from a web page), the clipboard may contain multiple formats, and the command-line tools will typically return only the plain text version.
Frequently Asked Questions
How do I copy a file’s content to the clipboard without adding a newline?
On Windows, use Get-Content file.txt -Raw | Set-Clipboard in PowerShell. On macOS, pbcopy < file.txt does not add a newline. On Linux, xclip -selection clipboard < file.txt also preserves the file exactly.
Can I copy binary data to the clipboard from the command line?
Generally, the clipboard is designed for text, but some tools can handle images. On macOS, pbcopy can copy image data if you use the correct pasteboard type, but it is not straightforward. On Linux, xclip can copy images if you specify the target, but it is rarely used. For most scripting purposes, stick to text.
Why does xclip not work on my Linux system?
If you are using a Wayland session, xclip will not work because it relies on X11. Install wl-clipboard and use wl-copy and wl-paste instead. You can check if you are on Wayland by running echo $XDG_SESSION_TYPE.
Is there a way to clear the clipboard from the command line?
Yes. On Windows, you can use powershell -command "Set-Clipboard -Value ''". On macOS, echo -n | pbcopy sets an empty clipboard. On Linux, echo -n | xclip -selection clipboard does the same.
Can I use these commands in a cron job or background script?
Yes, but on Linux with X11, the clipboard content may not persist after the script exits unless a clipboard manager is running. On macOS and Windows, the clipboard is system-wide and persists. For unattended scripts, consider writing the output to a file instead of the clipboard if persistence is an issue.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Samsung Smart Speaker Rollable Display: Facts
- HP 9730e: Wide Prints, Not A3 Scans
- What 8888 Does When You Use It as DNS Server
- Credential Manager: Seamless Sign-In Explained
- API Governance for Public Data: What Growing Portfolios Need
- Enterprise AI Hidden Cost: Data Architecture ROI