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

Re: Silly question on :h and = expansion



On 8/18/21, Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Tue, Aug 17, 2021 at 4:27 PM Zach Riggle <zachriggle@xxxxxxxxx> wrote:
>>
>> Awesome!!! These are all wonderful.  "=foo(:h)" is the most concise
>> way I see, and fits with my style.
>
> It might be worth noting:  That formulation only works in contexts
> where globbing is done, so not e.g. in conditionals.
>
> % [[ -f =zsh ]] && echo yes
> yes
> % [[ -d =zsh(:h) ]] && echo yes || echo no
> no
> % [[ -d ${${:-=zsh}:h} ]] && echo yes || echo no
> yes

You can use this form to enable globbing in conditionals,
% [[ -d =zsh(#q:h) ]] && echo yes || echo no
yes

but using = in conditionals is not very useful anyway since when it
fails it aborts, rather than returning an error (and (N) doesn't
suppress it since it's not a glob),
% [[ -f =bloo ]] || echo no
zsh: bloo not found!

-- 
Mikael Magnusson




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