3

In bash, echo toast | xclip -sel c saves the toast inside the clipboard, even when I close the terminal. Why does this seem to be non-persistent from other shells?

python3.5:

import os
os.system("echo toast | xclip -sel c")

Does the job, but when I press CtrlC, the clipboard is empty. A simple CtrlD does not clear the clipboard / works fine.

tclsh:

exec /bin/echo toast | xclip -sel c

Even keeps the process open, so I have to terminate it.

Is this a bug? How is this even possible? Or what am I misunderstanding?

Edit. cannot find a solution for this. I made a wish (Tcl/Tk)-script, included a exec /bin/echo toast | xclip -sel c line and closed the window. The clipboard is now empty. I tried -display, but both display and xauthority are set properly. The same problems were occuring with pythons pyperclip and xerox.

Edit2. the issue can be reduced to this question: https://unix.stackexchange.com/questions/316715/xclip-works-differently-in-interactive-and-non-interactive-shells and there does not seem to be a good alternative other than using xsel -ib for storing clipboards.

phil294
  • 10,038
  • 8
  • 65
  • 98
  • you may know this, but `exec` (at least in `(ba|k|c)*sh` creates a new process and overlays it on the existing. Maybe `tclsh` is different, but I would expect to lose access to ex-parent data in that case too. I don't know about `python`, but seems like the evidence supports the same intrepretation/idea/concept. Sorry I can't provide direct documentation about how `exec` works in shells, but if you search around a little bit you may find it. BUT happy to be corrected/enlightend by the +50K shell crowd. Good luck! – shellter Sep 02 '17 at 01:40

1 Answers1

2

try to continue pipeline or redirect output. For me its work in tcl:

exec printf "%s" $var | xclip -selection c | echo

or:

exec printf "%s" $var | xclip -selection c > /dev/null
hxss
  • 36
  • 3
  • Thanks, but for me this doesn't seem to do anything. https://i.imgur.com/Jz8mpA3.png – phil294 Nov 26 '17 at 04:47
  • In tclsh and scripts use `exit` command instead of Ctrl+C. https://i.imgur.com/YRtljGu.png – hxss Nov 26 '17 at 06:52
  • using exit is actually the key to this. note that instead of `| echo` or `> /dev/null`, job control with `&` will also do. thank you very much! – phil294 Nov 26 '17 at 07:23
  • Yes, but `&` dont wait for process end. https://i.imgur.com/81wOuDM.png – hxss Nov 26 '17 at 07:56