Skip to content

Instantly share code, notes, and snippets.

@aont
Created September 15, 2025 00:58
Show Gist options
  • Select an option

  • Save aont/dcaa0608384bb421f27be23113ebef08 to your computer and use it in GitHub Desktop.

Select an option

Save aont/dcaa0608384bb421f27be23113ebef08 to your computer and use it in GitHub Desktop.

Copying to the Clipboard with OSC52 in Bash

Copying text from a remote shell to your local clipboard can be tricky. Luckily, many terminals support OSC52, an escape sequence that lets you send data straight to the clipboard. Here’s a handy Bash function you can drop into your ~/.bashrc:

osc52() {
  local data
  data=$(openssl base64 | tr -d '\n')
  printf "\e]52;c;%s\a" "$data"
}

How It Works

  • Base64 encoding: openssl base64 converts the input to the required format.
  • Escape sequence: \e]52;c;<data>\a tells the terminal to put the decoded text into the clipboard.
  • Input via stdin: Just pipe anything you want to copy.

Usage Examples

echo "Hello" | osc52        # copy a string
cat file.txt | osc52        # copy a file
ls -la | osc52              # copy command output

Notes

  • Works in terminals like iTerm2, xterm, and gnome-terminal.
  • In tmux, you may need to enable set -g set-clipboard on.
  • Some systems restrict OSC52 for security reasons.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment