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

Re: [PATCH] typeset: set $? on incidental error



On 01/23/2016 06:53 PM, Daniel Shahaf wrote:
> Between all the replies I'm convinced that the builtin interface of
> 'local' shouldn't be changed: «builtin local x=$(false)» should set $?
> to 0.  (I meant to say that in a previous email.)
> 
> However, the question remains, in my eyes, whether the change should be
> made to the reserved word interface.  The reserved word 'typeset' is not
> a command, but part of the shell's language, so in
> .
>     typeset x=$(foo) y=$(bar)
> .
> the last command executed is 'bar'.  I would expect that line to behave
> as similarly to
> .
>     x=$(foo) y=$(bar)
> .
> as possible, so for example, I'd expect the former to set $? to the exit
> code of 'bar', as the latter statement does.
> 
> Cheers,
> 
> Daniel
> 

1(again): the other shells with typeset don't behave that way,
the chances of those shells seeing this a good idea and implementing it
are pretty slim in my opinion.

typeset originated in ksh and ksh93's development has pretty much stalled
over the past few years. When not following posix sh, bash tends to align with
the way ksh does things. needless to say that the other kshs tend to do the same.

Most likely resulting in one more way our typeset differs. zsh's typeset became
a reserved word recently in an attempt behave more like the other typesets.

2: typeset while now part of the `language', still retains it's own exit statuses
from when it was a builtin command.
''
typeset -T foo=bar baz=qux; echo $?; typeset -p foo baz
typeset: second argument of tie must be array: baz
1
typeset: no such variable: foo
typeset: no such variable: baz
''

So in the event that typeset _and_ your arbitrary commands errors, what should $?
be set to?
''
typeset -Z 1.1 foo=$(exit 42)
echo $? # 1 or 42?
''

3: in the `real world' if you are assigning a parameter via command substitution
and there is a possibility that the command substitution could return nothing.
You would get the parameter a default value. During assignment or during expansion.
''
typeset foo=${"$(exit 42)":-default}
# or
echo ${foo:=default} # or :-
''



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