Skip to content

Instantly share code, notes, and snippets.

@vifon
Created July 25, 2020 01:32
Show Gist options
  • Save vifon/a0c9c811e79e6d6ef580a04ac6d5ea57 to your computer and use it in GitHub Desktop.
Save vifon/a0c9c811e79e6d6ef580a04ac6d5ea57 to your computer and use it in GitHub Desktop.
Print to stdout each new X11 selection value separated by a single newline
#!/bin/bash
# Print to stdout each new X11 selection value. By default operates
# on "xsel -b". Any passed arguments are used instead of
# "-b" verbatim.
set -o errexit -o nounset -o pipefail
# Set argv to -b (operating on the CLIPBOARD selection) if
# not specified.
set -- "${@:--b}"
# Take control of the selection and wait until some other program
# takes it back, changing its value.
while xsel -i "$@" -n <<< '' > /dev/null; do
# Print the new selection value. $(…) is used to strip the
# trailing newlines, printf is used for adding exactly one
# newline afterwards.
printf '%s\n' "$(xsel "$@" -o)"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment