zsh-users
 help / color / mirror / code / Atom feed
From: Philippe Altherr <philippe.altherr@gmail.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org, Bart Schaefer <schaefer@brasslantern.com>
Subject: Re: trap question
Date: Sat, 26 Nov 2022 05:25:18 +0100	[thread overview]
Message-ID: <CAGdYchve984+y2j9eNq31K2eci6mb3SJ_0M4Rua=B=LmYg02_w@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7YQn7Qu6keQgzgT2K5S7QYb_K9wcrctWqYYCkaQv1ym0w@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2018 bytes --]

>
> You can think of "trap" as a delayed "eval".


The thing that you have to understand is that your string gets
evaluated/expanded twice: once when the trap statement is evaluated and
once each time the EXIT trap is triggered.

With your original code, when the trap statement is evaluated, the first
argument of trap (your string) gets expanded, like it would be for any
other command. This yields the string  [[ 0 == '0' ]] && echo trap: var is:
0. The trap command records this string as the new command to execute for
future EXIT traps. Thus, when an EXIT trap triggers, it will obviously
always print 0 rather than the current value of the variable "var".

Now, if you use single quotes instead of double quotes, the first argument
of the trap command expands to the string  [[ $var == '0' ]] && echo trap:
var is: $var, which looks like a much more interesting command to execute
when the EXIT trap triggers ;-)

Philippe


On Sat, Nov 26, 2022 at 2:58 AM Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Fri, Nov 25, 2022 at 3:25 PM Ray Andrews <rayandrews@eastlink.ca>
> wrote:
> >
> > Double quotes evaluated when
> > parsed, single quotes kept as literal until trap is sprung, yes?
>
> Yes.
>
> > wouldn't have occurred to me since I'd have expected the '$var' to
> > remain un-expandable within single quotes.  Is this an exception or
> > somehow to be viewed as routine?
>
> You can think of "trap" as a delayed "eval".
>
> > BTW, will this trap catch exiting
> > errors or just proper returns?
>
> Depends on the error.  If you have "setopt err_exit" (or err_return)
> then the trap will be tripped when something returns false, but if you
> have "setopt no_unset" (or use ${varname?message}) and reference a
> variable that isn't set, the trap will not trip.  There are also some
> circumstances where the parent shell will have done an opportunistic
> "exec" of the final external command, so there's no zsh left to run
> the trap (this is technically a bug, and will eventually get fixed).
>
>

[-- Attachment #2: Type: text/html, Size: 3291 bytes --]

  reply	other threads:[~2022-11-26  4:26 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-25 18:35 Ray Andrews
2022-11-25 18:57 ` Roman Perepelitsa
2022-11-25 23:25   ` Ray Andrews
2022-11-26  1:57     ` Bart Schaefer
2022-11-26  4:25       ` Philippe Altherr [this message]
2022-11-26  4:27       ` Ray Andrews
2022-11-26  4:49         ` Lawrence Velázquez
2022-11-26  4:50         ` Bart Schaefer
2022-11-26 15:03           ` Ray Andrews
2022-11-26 15:25             ` Roman Perepelitsa
2022-11-27  1:49               ` Ray Andrews

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='CAGdYchve984+y2j9eNq31K2eci6mb3SJ_0M4Rua=B=LmYg02_w@mail.gmail.com' \
    --to=philippe.altherr@gmail.com \
    --cc=rayandrews@eastlink.ca \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /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).