arrays - Why does read -a fail in zsh -


if type:

echo "1 dquote> 2 quick dquote> 3 brown" | while read -a d; echo "${d[1]}--${d[0]}"; done 

in bash says:

the--1 quick--2 brown--3 

but in zsh says:

zsh: bad option: -a 

why? , should instead?

in both shells read builtin. shares same purpose, implementation , options differ.

in order read in array in zsh, read requires option -a (instead of -a):

echo "1 2 quick    3 brown" | while read -a d; echo $d[2]--$d[1]; done  

note: there many more differences between zsh , bash:

  • in zsh arrays numbered 1 default, in bash start zero.
  • echo $array prints outputs elements in zsh first element in bash
  • to print third element of array in sh can use echo $array[3]. in bash braces needed delimit subscript, subscript third element 2: echo ${array[2]}.
  • in zsh not need quote parameter expansions in order handle values white spaces correctly. example

    filename="no such file" cat $filename 

    will print 1 error message in zsh:

    cat: 'no such file': no such file or directory 

    but 3 error messages in bash:

    cat: no: no such file or directory cat: such: no such file or directory cat: file: no such file or directory 
  • in zsh builtin echo evaluates escape codes default. in bash need pass -e argument that.

    echo 'foo\tbar' 

    zsh:

    foo     bar 

    bash:

    foo\tbar 

generally, important keep in mind that, while zsh , bash similar, far being same.


Comments