zsh-users
 help / color / mirror / code / Atom feed
* Interpret Parameter Value at Time of Function Definition
@ 2017-09-05 13:47 Vin Shelton
  2017-09-05 16:17 ` Daniel Shahaf
  2017-09-05 18:21 ` Martijn Dekker
  0 siblings, 2 replies; 6+ messages in thread
From: Vin Shelton @ 2017-09-05 13:47 UTC (permalink / raw)
  To: zsh-users

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

I think zsh interprets the value of a shell variable when a function is
run, but I would like to set the value when the function is read.

E.g:

$ aaa=bbb
$ function t {
    print aaa = \"$aaa\"
  }
$ t
aaa = "bbb"
$ aaa=ccc t
aaa = "ccc"

I would like to interpret $aaa at read time, so that:

    aaa=ccc t

would print:

    aaa = "bbb"

How do I do that?

Thanks,
  Vin

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Interpret Parameter Value at Time of Function Definition
  2017-09-05 13:47 Interpret Parameter Value at Time of Function Definition Vin Shelton
@ 2017-09-05 16:17 ` Daniel Shahaf
  2017-09-05 20:51   ` Bart Schaefer
  2017-09-05 18:21 ` Martijn Dekker
  1 sibling, 1 reply; 6+ messages in thread
From: Daniel Shahaf @ 2017-09-05 16:17 UTC (permalink / raw)
  To: Vin Shelton, zsh-users

Vin Shelton wrote on Tue, 05 Sep 2017 09:47 -0400:
> $ aaa=bbb
> $ function t {
>     print aaa = \"$aaa\"
>   }
> 
> I would like to interpret $aaa at read time, so that:
> 
>     aaa=ccc t
> 
> would print:
> 
>     aaa = "bbb"

Declare aaa local inside the function:

t() {
  local aaa=bbb
  typeset -p aaa
}
aaa=ccc t

... but then, if t() changes $aaa the changes won't be visible outside t().


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Interpret Parameter Value at Time of Function Definition
  2017-09-05 13:47 Interpret Parameter Value at Time of Function Definition Vin Shelton
  2017-09-05 16:17 ` Daniel Shahaf
@ 2017-09-05 18:21 ` Martijn Dekker
  2017-09-06  3:08   ` Ray Andrews
  1 sibling, 1 reply; 6+ messages in thread
From: Martijn Dekker @ 2017-09-05 18:21 UTC (permalink / raw)
  To: zsh-users

Op 05-09-17 om 15:47 schreef Vin Shelton:
[...]
> $ aaa=bbb
> $ function t {
>     print aaa = \"$aaa\"
>   }
> $ t
> aaa = "bbb"
> $ aaa=ccc t
> aaa = "ccc"
> 
> I would like to interpret $aaa at read time, so that:
> 
>     aaa=ccc t
> 
> would print:
> 
>     aaa = "bbb"
> 
> How do I do that?

AFAIK, the only way to do this without side effects is by using 'eval':

aaa=bbb
eval "function t {
  print \"aaa = $aaa\"
}"
typeset -f t

The value of "$aaa" is now hard-coded in the function definition, as you
can see by the output of 'typeset -f t'.

Note that you have to be very careful to backslash-escape everything
except $aaa correctly so that only $aaa is interpreted at read time,
otherwise you're going to get hard-to-trace bugs.

Also, this should ONLY be done this way if $aaa is guaranteed to contain
trusted data only. If the value of $aaa can be given by other users from
the outside, you've got a code injection vulnerability, as any shell
grammar in $aaa will be interpreted and executed by 'eval'. For
instance, setting
   aaa='"; echo injection"'
would print "injection".

You can avoid the vulnerability by shell-quoting the value of $aaa
before using it, which in zsh is easy to do using the (q) parameter
expansion flag:

# ... $aaa contains some untrusted data ...
eval "function t {
  print -r \"aaa =\" ${(q)aaa}
}"
typeset -f t

The expansion ${(q)aaa} is appropriately quoted as a string using shell
grammar so it will never be interpreted as code.

This is advanced stuff, so be careful.

- Martijn


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Interpret Parameter Value at Time of Function Definition
  2017-09-05 16:17 ` Daniel Shahaf
@ 2017-09-05 20:51   ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2017-09-05 20:51 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Vin Shelton, Zsh Users

On Tue, Sep 5, 2017 at 9:17 AM, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> Vin Shelton wrote on Tue, 05 Sep 2017 09:47 -0400:
>> $ aaa=bbb
>> $ function t {
>>     print aaa = \"$aaa\"
>>   }
>>
>> I would like to interpret $aaa at read time [...]
>
> Declare aaa local inside the function:

That won't help if the value assigned to $aaa is itself going to
contain expansions.

As Martijn points out, the only way to get this to work is with some
kind of "read-time" evaluation.  Exactly what approach is best depends
on what you want to accomplish.  Does $aaa expand to executable
statements that you want to turn into part of the function code, or
does it just result in a value that you want to access later?

No matter which way you go, the key is that you'll need to quote the
function body differently than you quote the value you want assigned
to $aaa.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Interpret Parameter Value at Time of Function Definition
  2017-09-05 18:21 ` Martijn Dekker
@ 2017-09-06  3:08   ` Ray Andrews
  2017-09-06  4:16     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Ray Andrews @ 2017-09-06  3:08 UTC (permalink / raw)
  To: zsh-users

On 05/09/17 11:21 AM, Martijn Dekker wrote:
>
>>      aaa=ccc t

How is it that that works without a semicolon between the commands?

BTW, when I've had a problem like this one I create files named after 
the variable and containing the content of the variable.  It's brutal 
and slow and stupid but it does provide absolute protection even on 
Thursdays because it bypasses all the intricacies of the rules about 
variables.  You hafta 'rm' the file to make the variable go away.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Interpret Parameter Value at Time of Function Definition
  2017-09-06  3:08   ` Ray Andrews
@ 2017-09-06  4:16     ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2017-09-06  4:16 UTC (permalink / raw)
  To: Zsh Users

On Tue, Sep 5, 2017 at 8:08 PM, Ray Andrews <rayandrews@eastlink.ca> wrote:
>
>>>      aaa=ccc t
>
> How is it that that works without a semicolon between the commands?

This is the standard syntax for temporarily placing the variable "aaa"
into the environment of the command "t".


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-09-06  4:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-05 13:47 Interpret Parameter Value at Time of Function Definition Vin Shelton
2017-09-05 16:17 ` Daniel Shahaf
2017-09-05 20:51   ` Bart Schaefer
2017-09-05 18:21 ` Martijn Dekker
2017-09-06  3:08   ` Ray Andrews
2017-09-06  4:16     ` Bart Schaefer

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).