zsh-workers
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: greg@klanderman.net, zsh-workers@sunsite.auc.dk (Zsh list)
Subject: Re: TRAPEXIT question
Date: Wed, 4 Aug 1999 04:40:26 +0000	[thread overview]
Message-ID: <990804044026.ZM12690@candle.brasslantern.com> (raw)
In-Reply-To: <14247.14862.404409.655828@pdx.itasoftware.com>

On Aug 3,  2:50pm, Greg Klanderman wrote:
} Subject: TRAPEXIT question
}
} Unfortunately, it seems that if TRAPEXIT is defined from
} within a shell function or even from a "source"d file, the exit 
} hook gets called when the function exits or sourced file finishes

Yes, this is correct.  However, how that trap behaves depends upon how
you define it.  To quote the manual:

     Note that traps defined with the `trap' builtin are slightly
     different from those defined as ``TRAP'NAL () { ... }', as the
     latter have their own function environment (line numbers, local
     variables, etc.) while the former use the environment of the
     command in which they were called.  For example,

     `trap 'print $LINENO' DEBUG'

     will print the line number of command executed after it has run,
     while

     `TRAPDEBUG() { print $LINENO; }'

     will always print the number zero.

This has an interesting side-effect in the case of the EXIT trap.  If you
use the `TRAPEXIT() { ... }' function form, then other traps set within the
trap itself have the context of the TRAPEXIT function; but if you use the
`trap "..." EXIT' form, traps set within the trap have as their context a
function that has already exited -- and as a consequence they get set in
the next context up, which can even be the top-level shell.

(I don't know if any other shell behaves this way; I know bash does *not*.
The new LOCAL_TRAPS option in 3.1.6 doesn't appear to affect it.)

Anyway, this means that either of the following forms create a function
that will install an exit trap on the calling shell:

	trip() { trap 'trap "echo trip" EXIT' EXIT }
	trip() { trap 'TRAPEXIT() { echo trip }' EXIT }

Which one you use probably depends on how hairy you want the quoting to
get.  It might be easiest to define yet another function that is the real
body of the trap, and simply call it:

	troll() { print "Who's that trip-trapping over MY bridge?" }
	trip() { trap 'trap troll EXIT' EXIT }

Now that I've answered the question ... can you tell me what's wrong with
using a .zlogout file instead?

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


  reply	other threads:[~1999-08-04  4:42 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-08-03 18:50 Greg Klanderman
1999-08-04  4:40 ` Bart Schaefer [this message]
1999-08-04  7:47   ` Peter Stephenson
1999-08-04 21:54   ` Greg Klanderman
1999-08-05  8:34     ` Peter Stephenson
1999-08-05 10:51       ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=990804044026.ZM12690@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=greg@klanderman.net \
    --cc=zsh-workers@sunsite.auc.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).