Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: can we detect the closing of a terminal?

On Fri, Sep 4, 2015 at 8:07 PM, Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:

> On 09/04/2015 02:37 PM, Kurtis Rader wrote:
>> I'm guessing that by "open a terminal" you mean start an interactive
>>> shell.
>> When I fire up an xterm it comes with a zsh shell pre loaded. That's all
> I know. In each one of them you can "echo $TTY" and it will report
> "/dev/pts/2" or some other number than '2'.  I can have a dozen of them
> open, and when I close one I'd like some way of letting the other terminals
> know it. Reason being that each terminal has an associated variable
> 't[number of the terminal]' set to its $PWD and I do stuff like: 'cp *
> $t10' where '$t10' is the current directory on terminal '/dev/pts/10' and
> which does horrible things is terminal #10 has been closed so I want some
> way of killing '$t10' when that happens.

Xterm does not "come with a zsh shell pre loaded". The xterm program by
default runs whatever program is referred to by your SHELL environment
variable. If that environment variable does not exist it will attempt to
use the shell defined for you account. Which, on a UNIX like system, is
typically in your /etc/passwd file or equivalent; I have no idea what the
equivalent is on a MS Windows system. However, you can tell xterm to run
any "shell" you wish. In fact, the program doesn't have to be an
interactive shell. For example, create a file named "xterm_ls" in your home
directory that contains

echo "TTY=$TTY"
/bin/sleep 10

Make it executable by running "chmod 755 ~/xterm_ls". Then, from an already
open shell, type "xterm ~/xterm_ls". You should see an xterm window appear
that shows the files in your home directory for ten seconds then
disappears. That "terminal" won't have a TTY variable because the TTY
variable is an artifact of a zsh interactive shell. And since that command
did not instantiate an interactive zsh instance there won't be a TTY
environment variable.

Here's another experiment. In an existing "terminal" type the following


You should get an xterm "terminal" that displays a Bourne shell prompt
rather than your Zsh prompt. After exiting the xterm you just created
you'll want to run


so that the terminal in which you ran this experiment continues to behave
as expected.

Lastly, you say that

each terminal has an associated variable 't[number of the terminal]' set to
> its $PWD

That is not true. The device path name for stdin and stdout of an
interactive shell depends on both the operating system and how it was
invoked. It might be /dev/pts/2 or /dev/tty or /dev/argle/bargle. You are
using premises that only hold true for the very constrained situation of
interest to yourself today. If you upgrade your operating system tomorrow
(or switch to a different UNIX like OS or a non UNIX like OS) there is a
strong possibility that your scripts will break.

Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

Messages sorted by: Reverse Date, Date, Thread, Author