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

Re: Bash to Zsh Funny



In the last episode (Mar 09), J said:
> > gvim.exe $files &
> 
> When you execute this line, $files is one space-separated string.
> zsh expands this to only one string and doesn't perform
> word-splitting, and that's what you expect most of the time. bash on
> the other hand performs word-splitting.
> 
> Compare results of:
> $ a='b c'
> $ for i in $a; do echo $i; done
> ...in both zsh and bash.
> 
> If you want to activate word-splitting in zsh, you can ask for it
> specifically for this expansion with ${=var}, or you can setopt
> shwordsplit to activate it for all expansions.

I think using arrays to store filenames is more natural:

  files=( *.txt )

which will preserve spaces within the filenames.  Note that you can
also do things like

  files=$( grep --null -l mytext * )
  files=( ${(ps:\0:)files} )

, to get a string of null-delimited filenames, then split on the null
to get your array.  Note that $(find . -name 'note???.txt') is
redundant; just use zsh's globbing directly.  If you know the resulting
filenames won't contain spaces:

  files=($(grep -il "note [0-9][0-9][0-9].*$1" note???.txt))

-- 
	Dan Nelson
	dnelson@xxxxxxxxxxxxxxx



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