I noticed that for the specific case of picking an element from the end using just the size of the array itself, the above is a somewhat contrived example; a plain -1 would work just as well. But I can change the example script as below, and the difference between outputs still holds:
#!/bin/bash
array=(
"a"
"b"
"c"
)
i=3
value="${array[i - 1]}"
printf "%s\n" "${value}"
With the same arguments given, output differs between 3.12.0 and 3.13.0, as demonstrated in the example below. I didn’t see this mentioned directly in the release notes, so I was wondering if this is an intentional change or a bug. I don’t mind the change per se, but just wanted to be sure that it’s permanent before I adjust all my existing scripts to conform to the newer way shfmt prefers this.
$ cat asdf
#!/bin/bash
array=(
"a"
"b"
"c"
)
value="${array[${#array[@]} - 1]}"
printf "%s\n" "${value}"
$ /usr/local/bin/archive/shfmt/shfmt_v3.12.0_linux_amd64 -i 4 -ci -l -d asdf # no output
$ /usr/local/bin/archive/shfmt/shfmt_v3.13.0_linux_amd64 -i 4 -ci -l -d asdf
asdf
diff asdf.orig asdf
--- asdf.orig
+++ asdf
@@ -6,6 +6,6 @@
"c"
)
-value="${array[${#array[@]} - 1]}"
+value="${array[${#array[@]}-1]}"
printf "%s\n" "${value}"