I want to expand a glob in zsh into only the filenames, rather than paths, of the matching files. I know that I can do something like this:
paths=(/some/path/blah*blah*blah)
typeset -a filenames
for i ({1..$#paths}); do
filenames[$i]=`basename $paths[$i]`
done
But I think there must be a better way.
There is a two-step process that uses parameter modifiers:
paths=(/some/path/blah*blah*blah)
filenames=($paths[@]:t)
but you can also apply the :t
modifier directly to the glob itself:
filenames=( /some/path/blah*blah*blah(:t) )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With