ksh: Array slicing incorrect result

This

a=(1 2 3); len=${#a[@]}; echo "${a[*]:0:$len}";

prints 1 2 3. But this

a=(1 2 3); echo "${a[*]:0:${#a[@]}}";

prints 1 1 2.

Testing envivonment:

$ ksh --version
  version         sh (AT&T Research) 93u+m/1.1.0-alpha+a86a7c0b/MOD 2023-03-04

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 17

Commits related to this issue

Most upvoted comments

Regardless of whether a slicing operation on an associative array is meaningful or not, as long as

typeset -A arr=(a (b d e) c)
num=${#arr[@]}
echo ${arr[@]:0:num}

and

typeset -A arr=(a (b d e) c)
echo ${arr[@]:0:${#arr[@]}}

don’t produce identical results, there is still a bug and I’m not happy.

$ ksh +E -c 'len=3; arr=(1 2 3 4 5); echo ${arr[@]:0:len}'
1 2 3
$ ksh +E -c 'i=(1 2 3 4); arr=(1 2 3 4 5); echo ${arr[@]:0:i[3]}'
1 2 3 4

That one does not show a bug; both commands are as expected. In the second one, i[3]==4 (since array indexes start at 0), so 1 2 3 4 is the expected output.