Sample Header Ad - 728x90

xclip works differently in interactive and non-interactive shells

5 votes
4 answers
3306 views
While investigating a problem described in [a question at stackoverflow](https://stackoverflow.com/questions/40061058/code-in-shell-script-runs-before-other-operations#comment67399516_40061058) I simplified it down to a test case demonstrating that in non-interactive mode bash seems to clear the X system clipboard before exiting. The test opens a gnome terminal and runs a bash script in it that places (via xclip) some text in the X system clipboard. While the terminal is open, querying the clipboard returns the text that was placed in it regardless of whether bash is run in interactive or non-interactive mode. However, after the terminal is closed, the clipboard contents survives if bash was run in interactive mode, but is lost if bash was run in non-interactive mode. $ cat xclip_test #!/usr/bin/env bash set -x gnome-terminal -x bash -i -c "echo abc|xclip -selection clipboard; sleep 3" sleep 1 xclip -o -selection clipboard sleep 4 xclip -o -selection clipboard gnome-terminal -x bash -c "echo 123|xclip -selection clipboard; sleep 3" sleep 1 xclip -o -selection clipboard sleep 4 xclip -o -selection clipboard $ ./xclip_test + gnome-terminal -x bash -i -c 'echo abc|xclip -selection clipboard; sleep 3' + sleep 1 + xclip -o -selection clipboard abc + sleep 4 + xclip -o -selection clipboard abc + gnome-terminal -x bash -c 'echo 123|xclip -selection clipboard; sleep 3' + sleep 1 + xclip -o -selection clipboard 123 + sleep 4 + xclip -o -selection clipboard Error: target STRING not available #!!!!!!!!!!!!! I am on Ubuntu 16.04, using default GNU bash (version 4.3.46(1)-release (x86_64-pc-linux-gnu)) with no customizations to bash rc files. I checked .bash_logout just in case and found a call to clear_console utility. However clear_console doesn't seem to deal with the clipboard; besides, the example doesn't run bash as a login shell. Is this something having a sensible explanation? **EDIT** The problem persists when replacing gnome-terminal with xterm: **gnome-terminal -x** ... --> **xterm -e** ... **&** Also it is not unique to bash - it is reproduced with dash, too.
Asked by Leon (191 rep)
Oct 16, 2016, 10:34 AM
Last activity: Dec 13, 2022, 07:30 PM