# pbcopy & pbpaste — Use the Clipboard from the Terminal

> pbcopy and pbpaste bridge the terminal and the macOS clipboard: copy command output, paste content and pipe it straight through your shell pipelines.

Source: https://www.jpkc.com/db/en/cheatsheets/macos/pbcopy/

<!-- PROSE:intro -->
`pbcopy` and `pbpaste` are the bridge between the terminal and the macOS clipboard: `pbcopy` reads from stdin and places the content on the clipboard, while `pbpaste` prints its content to stdout. That lets you copy command output into graphical apps in one move, feed clipboard content into scripts, or slot both tools into the middle of a pipeline. Both commands ship with macOS, so they are available instantly with nothing to install. This guide walks you through the moves you reach for every day.
<!-- PROSE:intro:end -->

## pbcopy — Copy to Clipboard

`echo '<text>' | pbcopy` — Copy text to the clipboard.

```bash
echo 'Hello World' | pbcopy
```

`cat <file> | pbcopy` — Copy file contents to the clipboard.

```bash
cat ~/.ssh/id_ed25519.pub | pbcopy
```

`<command> | pbcopy` — Copy command output to the clipboard.

```bash
pwd | pbcopy
```

`pbcopy < <file>` — Copy file contents using input redirection.

```bash
pbcopy < config.yaml
```

## pbpaste — Paste from Clipboard

`pbpaste` — Output clipboard contents to stdout.

```bash
pbpaste
```

`pbpaste > <file>` — Save clipboard contents to a file.

```bash
pbpaste > snippet.txt
```

`pbpaste | <command>` — Pipe clipboard contents to a command.

```bash
pbpaste | wc -l
```

`pbpaste | sort | uniq` — Process clipboard text through a pipeline.

```bash
pbpaste | sort | uniq
```

## Common Patterns

`cat ~/.ssh/id_ed25519.pub | pbcopy` — Copy SSH public key to clipboard.

```bash
cat ~/.ssh/id_ed25519.pub | pbcopy
```

`git diff | pbcopy` — Copy git diff to clipboard.

```bash
git diff | pbcopy
```

`pbpaste | python3 -m json.tool | pbcopy` — Pretty-print JSON from clipboard and copy back.

```bash
pbpaste | python3 -m json.tool | pbcopy
```

`echo -n '<text>' | pbcopy` — Copy text without trailing newline.

```bash
echo -n 'no newline' | pbcopy
```

`pbpaste | grep '<pattern>'` — Search clipboard contents.

```bash
pbpaste | grep 'error'
```

`pbpaste | tr '\n' ','` — Convert clipboard lines to comma-separated.

```bash
pbpaste | tr '\n' ','
```

<!-- PROSE:outro -->
## Conclusion

`pbcopy` and `pbpaste` are small helpers with a big payoff: they turn the clipboard into a first-class link in any shell pipeline and save you the chore of selecting text with the mouse. Keep in mind that `pbcopy` takes the data verbatim – text is stored in the encoding of the input (usually UTF-8), and `echo` appends a trailing newline by default, which you avoid with `echo -n`. Both tools exist only on macOS; on Linux, `xclip`, `xsel` or `wl-copy` fill the same role. If you live in the terminal, these two commands save you a surprising number of steps over the course of a day.

## Further Reading

- [pbcopy(1) – macOS man page](https://ss64.com/mac/pbcopy.html) – pbcopy reference at ss64.com
- [pbpaste(1) – macOS man page](https://ss64.com/mac/pbpaste.html) – pbpaste reference at ss64.com
<!-- PROSE:outro:end -->

## Related Commands

- [caffeinate](https://www.jpkc.com/db/en/cheatsheets/macos/caffeinate/) – prevents the Mac from going to sleep
- [defaults](https://www.jpkc.com/db/en/cheatsheets/macos/defaults/) – reads and writes macOS system preferences
- [diskutil](https://www.jpkc.com/db/en/cheatsheets/macos/diskutil/) – manages disks, partitions and volumes

