Search code examples
tclexecnewlineclipboard

Tcl: can catch { exec } know whether a final newline was output?


Consider the following:

% catch { exec echo "test" } result
0
% catch { exec echo -n "test" } resultnonl
0
% if { $result == $resultnonl } { echo "true" }
true

Question: Is there a way for the two resulting variables to be different?

Use case: I'm retrieving the contents of the clipboard and cannot differentiate between these two cases. In Emacs, it is very common for me to kill (cut) a line without its final newline, and also very common to kill a whole line. The clipboard only differs by the newline.


Solution

  • Check out the -keepnewline flag to exec. Watch:

    catch { exec -keepnewline -- echo "test" } result
    string length $result