From 4a98907202a683d79f6acfe0335a9911cb65d5ba Mon Sep 17 00:00:00 2001 From: Gerd Aschemann Date: Tue, 30 Jan 2024 01:23:54 +0100 Subject: [PATCH] #1273 Fix array indexing and length handling for sdk ls Cf. https://sdkman.slack.com/archives/CJTNQA94M/p1706573532033689?thread_ts=1706516271.626039&cid=CJTNQA94M - Array indexing is wrong, it starts at 1 (not 0) and ends at the highest index, not at array length -1. You have both in C or Java, but shell is different. - Executed in bash the `${#versions}` is - for whatever reason - restricted to four bits. So the maximum value is 15. If the versions array is > 15, you have to use `${#versions[@]}` instead (cf. https://linuxhandbook.com/array-length-bash/). --- src/main/bash/sdkman-list.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/bash/sdkman-list.sh b/src/main/bash/sdkman-list.sh index 5624120fc..733ecdbd1 100644 --- a/src/main/bash/sdkman-list.sh +++ b/src/main/bash/sdkman-list.sh @@ -76,7 +76,7 @@ function __sdkman_offline_list() { __sdkman_echo_no_colour "--------------------------------------------------------------------------------" local versions=($(echo ${versions_csv//,/ })) - for ((i = ${#versions} - 1; i >= 0; i--)); do + for ((i = ${#versions[@]}; i > 0; i--)); do if [[ -n "${versions[${i}]}" ]]; then if [[ "${versions[${i}]}" == "$CURRENT" ]]; then __sdkman_echo_no_colour " > ${versions[${i}]}"