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

Re: Bug on parameter expansion when set -u is in effect



On Thu, Feb 5, 2026 at 12:11 PM Ronald Fischer <ynnor@xxxxx> wrote:
>
> Using zsh 5.8.1 (x86_64-apple-darwin21.0)
>
> Consider the following fragment of code:
>
> kv="a b"
> k=${kv%% *}
> v=${kv##${k} #}
>
> This would set k to "a" and v to "b", and indeed usually does so.
>
> However, when I run this code with
>
>   set -u
>
> in effect, the variable v is set to "a b" instead of "b".
>
> I think this is a bug, because all variables are assigned a value before they are expanded, so -u should not have any effect.

It would be, but it is in fact not affected by set -u. Did you forget
to setopt extendedglob in your test environment?

% zsh -fc 'kv="a b" ;k=${kv%% *}; v=${kv##${k} #}; echo $v'
a b
% zsh -fc 'setopt extendedglob; kv="a b" ;k=${kv%% *}; v=${kv##${k}
#}; echo $v'
b
% zsh -fc 'setopt nounset extendedglob; kv="a b" ;k=${kv%% *};
v=${kv##${k} #}; echo $v'
b

-- 
Mikael Magnusson




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