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

Re: [ -d (#i)Temp ]



On Fri, Apr 10, 2026 at 10:53 AM S. Cowles <scowles@xxxxxxxxx> wrote:
>
> On Fri, 10 Apr 2026, Ray Andrews wrote:
> > From: Ray Andrews <rayandrews@xxxxxxxxxxx>
> > Subject: [ -d (#i)Temp ]
> > Is it possible to do something like this:?
> >  [ -d (#i)Temp ] && echo 'it's a directory'
> > ... that doesn't work

That should work.  It does for me (assuming NO_NULL_GLOB).  Try it
with setopt xtrace to see what's actually being executed.

A better construction might be to use the "test" builtin:

test -d (#i)Temp && echo "it's a directory"

You'll get an error code ($? == 2) if the glob returns more than one
match, or ($? == 1) when not a directory.  There is a possibility of
confusion if the glob returns a file name starting with a hyphen.

> [[ -d ${:-"$( echo (#i)Tmp )"} ]] && echo "it's a directory" || echo no such dir

You don't need the ${:-...} around that, but ...

> notes:
> 1)  single brackets are bash / sh (more overhead).  double brackets are the zsh builtin.

Single bracket is also a zsh builtin, effectively just an alias for
"test".  There's more overhead from $(...) than from the brackets.

> 2)  quoting on the return message.

Also not really necessary with [[ ]] and in fact ${:-...} forces a
single string replacement already.

Finally "[" and "test" do globbing on their arguments, "[[" does not,
which is why the $(...) is needed.




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