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

Re: trivial question



On Sun, Dec 4, 2022, at 9:05 PM, Ray Andrews wrote:
> Running this:
>
> var=( "now is" "the time" "for all good" "men to" )
>
> echo var[2] ain\'t nothing
> echo var2] ain\'t nothing
> echo "var[2]"
> echo $var[2]
>
> ... I get this:
>
> 4 /aWorking/Zsh/Source/Wk 0 $ . test1
> ain't nothing
> var2] ain't nothing
> var[2]
> the time
>
>
> ... It hardly matters but I'm curious that 'var[2]' without the dollar 
> sign evaluates to nothing at all.

In this context, "var[2]" is recognized as a valid glob, and zsh
attempts to generate filenames with it.  If a file or directory
matches, its name is inserted as usual.

	% cat foo.zsh
	var=('now is' 'the time' 'for all good' 'men to')

	printf '<%s>' var[2] ain\'t nothing
	echo

	% : >var2
	% zsh foo.zsh
	<var2><ain't><nothing>

If no file or directory matches, zsh generates an error by default.

	% rm var2
	% zsh foo.zsh
	foo.zsh:3: no matches found: var[2]

If NULL_GLOB is enabled (which you presumably have done), the failed
glob expands to nothing instead.

	% zsh -o NULL_GLOB foo.zsh
	<ain't><nothing>


> I have zero need for it to do 
> otherwise still I'm mildly expecting it to just end up as a plain string

This happens if NOMATCH is disabled.

	% zsh +o NOMATCH foo.zsh  
	<var[2]><ain't><nothing>

It is also the default behavior for failed globs in other shells.


> the same way 'var2]' does.

The latter is not a valid glob, so zsh does not attempt to generate
filenames with it.  It is just passed as a literal argument.


> Seems strange that it evaporates entirely.

This is all explained in the manual.

https://zsh.sourceforge.io/Doc/Release/Expansion.html#Filename-Generation


-- 
vq




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