keepcalculator

06 Numbers and Powers

Clipboard From Command Line: Copy & Paste in Terminal

Card No. 06·67854 · Updated · Standard formula, shown below

You can manipulate the clipboard directly from the command line using built-in utilities or small third-party tools. On Linux, xclip or xsel handle the X11 clipboard; on macOS, pbcopy and pbpaste are built-in; on Windows, clip and Get-Clipboard/Set-Clipboard in PowerShell do the job. This article explains the exact commands, how they work, and common pitfalls for each platform.

Why Use the Clipboard in a Terminal?

The clipboard is normally a graphical feature, but scripting it from the command line unlocks several practical uses:

  • Pipe output directly – save a command’s result (like a file listing or a log excerpt) straight into the clipboard without opening an editor.
  • Automate repetitive tasks – copy dynamic values (timestamps, generated passwords) into a clipboard for pasting into a web form or chat.
  • Integrate with scripts – a backup script can copy the path of the last saved file, or a build script can copy a version number.
  • Work on headless servers – with a remote desktop or X11 forwarding, you can still interact with the remote clipboard.

Once you know the right commands, the clipboard becomes just another stream you can pipe to and from, like stdout or a file.

macOS: pbcopy and pbpaste

macOS includes two commands that are as simple as they sound:

  • pbcopy – reads from standard input and places it on the clipboard.
  • pbpaste – writes the current clipboard contents to standard output.

Examples:

echo "Hello from terminal" | pbcopy
pbpaste
cat file.txt | pbcopy
pbcopy < file.txt

You can also copy the output of a command directly:

ls -la | pbcopy

That copies the directory listing as plain text. To paste it into a file, redirect pbpaste:

pbpaste > clipboard_content.txt

One subtlety: pbcopy and pbpaste work with the general clipboard by default. If you want to use the “find” pasteboard (used by the Find panel in many apps), add the -pboard find option:

echo "search term" | pbcopy -pboard find

Linux (X11): xclip and xsel

Most Linux distributions don’t include a clipboard utility by default, but two popular tools fill the gap:

  • xclip – provides xclip -selection clipboard to copy and xclip -o -selection clipboard to paste.
  • xsel – provides xsel --clipboard --input to copy and xsel --clipboard --output to paste.

Install them with your package manager, for example:

sudo apt install xclip   # Debian/Ubuntu
sudo dnf install xclip   # Fedora
sudo pacman -S xclip     # Arch

Basic usage with xclip:

echo "text" | xclip -selection clipboard
xclip -o -selection clipboard

With xsel:

echo "text" | xsel --clipboard --input
xsel --clipboard --output

Both tools also support the “primary” selection (the middle-click paste buffer in X11) if you omit the --clipboard flag or use -selection primary. That’s a separate clipboard that many users confuse with the main one.

Windows: clip and PowerShell

Windows has a built-in clip.exe that copies standard input to the clipboard. It works in Command Prompt and PowerShell:

echo Hello | clip
dir | clip

To paste from the clipboard in Command Prompt, there’s no direct built-in, but you can use PowerShell:

powershell -command "Get-Clipboard"

In PowerShell itself, you have two cmdlets:

  • Set-Clipboard – takes input and copies it to the clipboard.
  • Get-Clipboard – retrieves the clipboard content.

Examples:

Get-Process | Out-String | Set-Clipboard
Get-Clipboard

Note that Set-Clipboard can also handle non-text data like images, but the command-line examples above focus on text. For plain text, the classic clip.exe is still the simplest.

Clipboard in SSH and Remote Sessions

When you SSH into a remote Linux machine, the clipboard commands may not work because there’s no X server. In that case, you have a few options:

  • Use a terminal multiplexer – tmux or screen have their own copy-paste buffers that work over SSH.
  • Forward X11 – if you connect with ssh -X, the remote xclip can talk to your local X server, so the clipboard is shared.
  • Use a local clipboard bridge – tools like sshpass or custom scripts can copy the remote output to your local clipboard after the command finishes.

For most SSH users, the simplest approach is to use tmux and its copy mode (Ctrl-b [ to enter, then select and Ctrl-b ] to paste). That works entirely on the remote side and doesn’t need X.

Common Pitfalls and How to Avoid Them

  • Trailing newline – echo adds a newline by default. Use echo -n on Linux/macOS or Write-Output -NoNewline in PowerShell to avoid it.
  • Binary data – the clipboard is text-oriented. Copying binary data (like an image file) with cat image.png | pbcopy may corrupt it. Use dedicated tools like pngpaste on macOS or xclip -selection clipboard -t image/png on Linux.
  • Multiple selections on X11 – remember that “clipboard” and “primary” are separate. If you copy with xclip but paste with middle-click, you may see old content.
  • Empty clipboard – some commands return nothing when the clipboard is empty. Check with pbpaste | wc -c or xclip -o | wc -c to see if it’s truly empty.
  • Permissions – on Linux, xclip needs access to the X display. If you run it from a cron job or a service, it may fail. Use DISPLAY=:0 if needed, though that’s a security risk.

Frequently Asked Questions

How do I copy a file’s content without adding a newline?

Use cat file | tr -d '\n' | pbcopy (macOS) or cat file | tr -d '\n' | xclip -selection clipboard (Linux). The tr -d '\n' removes all newlines. If you only want to remove the final newline, use head -c -1 file | pbcopy (GNU head) or perl -pe 'chomp if eof' file | pbcopy.

Can I copy the output of a command that runs for a long time?

Yes, just pipe it. The command will run to completion before the clipboard is updated. For example, find / -name "*.log" 2>/dev/null | xclip -selection clipboard will wait until find finishes. If you want to see the output while it’s copying, you can use tee: find / -name "*.log" | tee /dev/stderr | xclip -selection clipboard.

Why does xclip not work in a script?

Usually because the script doesn’t have access to the X display. Add export DISPLAY=:0 at the top, but be aware that this exposes your X server to the script. Alternatively, use a tool like xsel which may have different behavior, or run the script from a terminal where DISPLAY is already set.

Is there a way to clear the clipboard from the command line?

Yes. On macOS, echo -n | pbcopy sets it to an empty string. On Linux, echo -n | xclip -selection clipboard does the same. On Windows PowerShell, Set-Clipboard -Value "" clears it. Some systems also have a xclip -selection clipboard -i /dev/null trick, but that may not work everywhere.

How do I copy a file’s path instead of its content?

Use realpath file.txt | pbcopy (macOS/Linux) or (Resolve-Path file.txt).Path | Set-Clipboard (PowerShell). The realpath command prints the absolute path, which you can then pipe to the clipboard tool.

Can I copy multiple lines from a command output?

Yes, the clipboard handles multiline text naturally. Just pipe the output as-is. For example, ps aux | head -20 | pbcopy copies 20 lines. When you paste, the line breaks are preserved.

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

The whole Numbers and Powers drawer →