From my experience with modern programming and scripting languages, I believe most programmers are generally accustomed to referring to the first element of an array by 0 as index.
Are there any substantial advantages in using 1?
I'm sure I've heard of more languages other than Zsh behaving similarly with arrays; it's fine by me, as it is equally convenient.
However, as previously released and widely used shell scripting languages such as ksh and bash all use 0, why would someone choose to alter this common "standard"?
My immediate answer to my question would be "of course not";
then, the only explanation I can think of regarding this somewhat "exclusive feature" to shells would be "they just did this to show off a bit more their cool shell".
I don't know much of either Zsh or its history though, and there is a high chance my trivial theory about this does not make any sense.
Is there an explanation for this? Or is it just out of personal taste?
Best Answer
awk
,tcl
at least, and tools typically used from the shell (cut -f1-3
,head -n 3
,sort -k1,3
,cal 1 2015
,comm -1
) start at 1.sed
,ed
,vi
number their lines from 1...$@
start at 1. zsh is consistent with its handling of$@
(like in Bourne) or$argv
(like in csh). See how confusing it is inksh
where${@:0:1}
does not give you the first positional parameter for instance.$a[1]
. It also means that the number of elements is the same as the last indice (in zsh like in most other shells except ksh, arrays are not sparse).a[1]
for the first element is consistent witha[-1]
for the last.So IMO the question should rather be: what got into David Korn's head to make its arrays start at 0?