Stage a token in the clipboard (Linux/X11)
Stage a secret (token, key, password) into the clipboard from a terminal so it can be pasted into a target app, without writing it to a file or shell history.
When to use
- You need to hand a credential to a GUI app (browser form, editor, chat) and the source lives in a terminal or script.
- The token must transit the clipboard only — never a temp file, never an echoed command line in shared logs.
Procedure
- Make sure
xclipis installed:command -v xclip— if missing,sudo apt-get install -y xclip. - Stage the value, reading it from wherever it lives (env var, file, command
output):
printf '%s' "$TOKEN" | xclip -selection clipboard - Verify the clipboard content without printing it to shared logs:
xclip -selection clipboard -o | wc -c(length check), or paste into the target app directly. - Switch to the target app and paste (
Ctrl+V).
Edge cases
- No X display (headless box, SSH):
xclipfails withCan't open display. Checkecho $DISPLAY; if empty or stale, start one and target it:Xvfb :99 -screen 0 1920x1080x24 &thenexport DISPLAY=:99(the clipboard then lives inside that X session only). - Wayland sessions: use
wl-copy/wl-pasteinstead ofxclip. - Values with single quotes or newlines: prefer reading from an env var or file over inlining the literal into the command.
- Clipboard managers may keep the value after the session ends; clear it when
done:
printf '' | xclip -selection clipboard. xclipforks and returns immediately on some setups; if the paste lands empty, retry once — the first invocation may still have been staging.