From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 2477 invoked from network); 26 Nov 2022 04:26:21 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 26 Nov 2022 04:26:21 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1669436781; b=I1GIKwtHzFWKqPeGuObdkOL9yQHODRy6cBNGcOio9sShLe1EB31SdP45qsVSjkCHnJvrPXxL4+ /wlX6tAA9x+OkgQLQxHzJbdcWgjyDgD7+jkmZ6YdrKbct2Hj2fMvNpFX6se/lvLa9raRehx8hq /R71hJEvQvT6yQCVfss+KpLCOyPOt78/Pl9AWG43erf4Kjo6LamXWU2pJZ8vXk4DH2lWBfVr/U opFXKwpB9yAjdmFDod8onHwmk06atJngLoyiydJ9g1zFKJuNFdAYygcyCIc5pYU/RdNZfLL5/3 wG7gXLmFuOhtULbyVILVBVrXNnz3sRagd6d3lvFSTQ8w0A==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-vs1-f49.google.com) smtp.remote-ip=209.85.217.49; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1669436781; bh=dWtj/iZTs5Y0JazSB89Dt/UC2IRRnb6nmsrs/e0n3gw=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID:Date:From:In-Reply-To: References:MIME-Version:DKIM-Signature:DKIM-Signature; b=FVdJfK3UXIeQD9scVNUISQ7IXFT7o/4Kpg9VZgOZ76v2To9voSyHxPTPRBAfa06paRgHRgm8BD YUc4ADFjCF8yXpQyx9SuzsEAxz35W9sJi/V3qBGHdyRoYoqL3aj7fkjLhkjUTbuEEBe+oGDxEb YbOzusuuiFNxtbjoqbJj9WWnKJm7h/AvFjYVCeKhBsDPs9R+DxLl8oNQPU4GFO1tCySxBMt5vS LgyCv/ifGmCQB05iLy8K0WaQztDuzr2F3k/7BLxELYWYXuWxFbj3Uc9CtOUKgtRT3qz+ewlvOt Dve7n4u782YZLp71HtQx8o8Kpgep+gBkAcm591nwiZJhog==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:Cc:To:Subject:Message-ID :Date:From:In-Reply-To:References:MIME-Version:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=ovR2XCPUimUD/TMMzr6WG06tpVdEtiUr2XsDvRuznR8=; b=dv4S1ho4PRDREyOQe7rKR+SkeB CVt9/F/PqLuEWLjWhrobQV/hLM4+zzo4xl6xu3xVafdSCdUHbGRPyJVsr5jHpSWG4A/qBa1W5OYJF J6s8yaVkGmY4qUI4CdZODjIWBVvtEyCokEEZSlsbSnGWjOuigTqds8mmKR8IXtomm5DnSdWKd+GUi xPRXtipLvtQLpFPQEZe78pGkHK958gBC6Un0KrVOPfcFA/sU/EMcXiXGk2IyWeX5YXQLecF1OPqpi R+pFQ1EsJ7naUgAnUXwrmAh0lh1mHDBAgA3Du3YPVyI3JECef1lUklnGFTg1ggwSKQd9FLRn2Fudf CZVX8vDg==; Received: by zero.zsh.org with local id 1oymlh-0002TP-KW; Sat, 26 Nov 2022 04:26:21 +0000 Authentication-Results: zsh.org; iprev=pass (mail-vs1-f49.google.com) smtp.remote-ip=209.85.217.49; dkim=pass header.d=gmail.com header.s=20210112 header.a=rsa-sha256; dmarc=pass header.from=gmail.com; arc=none Received: from mail-vs1-f49.google.com ([209.85.217.49]:46928) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1oymks-0001md-Bm; Sat, 26 Nov 2022 04:25:31 +0000 Received: by mail-vs1-f49.google.com with SMTP id v128so5833068vsb.13 for ; Fri, 25 Nov 2022 20:25:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=ovR2XCPUimUD/TMMzr6WG06tpVdEtiUr2XsDvRuznR8=; b=SQ+Yxpbmb9H6N41ioxqb4/H48RKXJCxAghjI4wQyWlhRj0wAhabRCiR5XgQatCyvGq Yiy1kiYyl/CQYgi9F5qqP2xjCm2l9fDbr4aAOAqhxWL9X6LVZZbpASBq8e98zOlGYGwa fu+Y9lagKs3MesLbfHzHnRk6P/IFZWiebwN6rmipBBzcgwtrHVJKefTT+iSijET/yKP6 0iLI/zYM3obyxlsCC9fDnBYsjw/ZRqvL9KuCP0iy7ofqBOS0V6huw3CfZJFUgDpylp+K K2DSEgn67y4DHhYeKvyhmCZpNDWkxmIhkGTeIUgbkp3xcFTcNnlWXAhjbQLAdlubswzn 6tIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ovR2XCPUimUD/TMMzr6WG06tpVdEtiUr2XsDvRuznR8=; b=h65nrq4I6IW0YhQ25YgdR1NekYQbGbYhP0htJb7GdoiMEImZbrXNRB1DjQvdifds9w Ak6zPxOy7JBGzQpue2Z4AMBXVPJczPK7yECaE/TzjMJls+o5GGuLHM5ma34ffNn5jwD1 NukJQHEtOuq1CH46blAkH4kNuDVr6BwW8zl4S9S9p9/ZlnVV1oVUUTegsYAnRnt4KRdD pAw2ojMJK0ERQvrUfZYLFeUOMKVaQy240NjWjA6hiE+cFvresOA6Aixy9vu0pQEngRzg uANx8dY5ywM0gaIxKajGJntfnoLs5FUvMfuKMoA6vhUhJOYY2FIEbL2BYgtTvY+f/nN1 D/gA== X-Gm-Message-State: ANoB5pnwdNN9WPcaA5M2/TcDVxcFgt8sZ/YsKdHPnqkFJaYu+Vs/qCe1 Kp0oJcrTt6GLDqcKF2k4PQ9SR3OagHoSNqNuyjQF/SplNtM= X-Google-Smtp-Source: AA0mqf6X0cimf3va8ixrxWo4JAxRgaBZ2rYZFk+26xr6KGF0ytLrkYLn+J/8FWTg9yp5Al3myXrBiM4KwNTIkGs1F3E= X-Received: by 2002:a05:6102:3d98:b0:3ad:24a7:63fa with SMTP id h24-20020a0561023d9800b003ad24a763famr16835584vsv.51.1669436729217; Fri, 25 Nov 2022 20:25:29 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Philippe Altherr Date: Sat, 26 Nov 2022 05:25:18 +0100 Message-ID: Subject: Re: trap question To: Ray Andrews Cc: zsh-users@zsh.org, Bart Schaefer Content-Type: multipart/alternative; boundary="0000000000003858cf05ee5807c9" X-Seq: 28431 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: --0000000000003858cf05ee5807c9 Content-Type: text/plain; charset="UTF-8" > > 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 wrote: > On Fri, Nov 25, 2022 at 3:25 PM Ray Andrews > 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). > > --0000000000003858cf05ee5807c9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
You can = think of "trap" as a delayed "eval".
<= br>
The thing that you have to understand is that your string get= s evaluated/expanded twice: once when the trap statement is evaluated and o= nce each time the EXIT trap is triggered.

With you= r original code, when the trap statement is evaluated, the first argument o= f trap (your string) gets expanded, like it would be for any other command.= This yields the string=C2=A0=C2=A0[[ 0 =3D=3D '0' ]] && ech= o trap: var is: 0. The trap command records this string as the new c= ommand 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 varia= ble "var".

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

Philippe


On Sat, Nov 26, 2022 at 2:58 AM Bart Schaefer <schaefer@brasslantern.com> wro= te:
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.=C2=A0 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.=C2=A0 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.=C2=A0 There are also s= ome
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).

--0000000000003858cf05ee5807c9--