2

I'm new to zsh and have discovered some strange behavior, but this tops it all.

I just came across this extremely strange behavior when declaring a local array in a zsh function that I was writing for my .zshrc file. When I declared a local array it caused my prompt to go crazy. I eventually traced it back to two very strange functions that randomly got created. I reduced it to a simple case:

% function () { local foobar=(); echo frog; }
% echo ${functions[foobar=]}
        echo frog
% echo ${functions[local]}
        echo frog

The first strange thing is that frog never gets printed from the anonymous function. But the really strange thing is that instead two functions named foobar= and local get created.

This causes all kinds of problems for my zsh prompt. Whenever the local keyword is used it printed frog.

I am really wondering what on earth caused zsh to create those functions.

I am using zsh 5.0.2

1 Answer 1

1

I found that it must have been a bug in my version of zsh. I upgraded to zsh 5.6.2 and found that the behavior was fixed in that version. I also have a theory as to what zsh was trying to do in the problem situation. When I run:

% foo bar() echo hi

It creates two functions, foo and bar, both of which simply echo hi.

I think that zsh had been interpreting local foobar=() as defining two functions: local and foobar=. This is really bizarre, as I didn't think that local or foobar= would be valid function names. I'd be curious to see where the bug was in zsh source.

I also found that the proper way to declare a local array is local -a foobar

1
  • 1
    How about you accept your own answer, so this question doesn't stay marked an "unanswered"? Thanks! Nov 8, 2021 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .