Skip to content

Instantly share code, notes, and snippets.

@SuggonM
Last active August 9, 2026 16:55
Show Gist options
  • Select an option

  • Save SuggonM/f08fb33a613601183c6354d193a83f00 to your computer and use it in GitHub Desktop.

Select an option

Save SuggonM/f08fb33a613601183c6354d193a83f00 to your computer and use it in GitHub Desktop.
Read clipboard with Termux API without it running in the foreground
#!/data/data/com.termux/files/usr/bin/bash
clipboard=$(termux-clipboard-get)
if [ -z "$clipboard" ]; then
am startservice com.termux.window/.TermuxFloatService &> /dev/null
sleep 0.1
clipboard=$(termux-clipboard-get)
am stopservice com.termux.window/.TermuxFloatService &> /dev/null &
fi
echo "$clipboard"

Concept from my reply in termux/termux-api#452 (comment). It spawns a Termux:Float instance to temporarily gain foreground activity, allowing Termux to read the system clipboard.

This is currently the most efficient workaround, according to Benjamin-Loison/termux-api#32.
Check out Benjamin_Loison/ADB_clipboard !

Requirements

Usage

Save the clipboard-get.sh into your device. Chmod it and run it with ./clipboard-get.sh.

If you're SSH'ing into Termux from a different machine, use ssh termux ./clipboard-get.sh. Or if the script isn't installed, use:

ssh termux bash << 'EOF'
# ... clipboard-get.sh body ...
EOF

The script takes about 1-3 seconds to complete.

For better consistency, you may need to fine-tune the sleep time in line 6, depending on your system performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment