SC2048 "${array[@]:-}" pitfall with empty arrays and set -u
here is a pitfall with empty arrays and set -u and recommendation from SC2048 "${array[@]}" or "${array[@]:-}" .. the behavior changes because with quotes it will run at least 1 time now.
For bugs
- Rule Id : SC2048
- My shellcheck version (
shellcheck --versionor "online"): online - [x] The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2048)
- [x] I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit
#!/usr/bin/env bash
set -u
declare -a array=()
for item in ${array[@]}; do
printf "throws error - unbound variable \n\n"
done
for item in "${array[@]}"; do
printf "throws error - unbound variable \n\n"
done
for item in ${array[@]:-}; do
printf "will not run \n\n"
done
for item in "${array[@]:-}"; do
printf "will run at least ONE time\n\n" #not intended !!
done
for item in ${array[@]+"${array[@]}"}; do
printf "would be correct\n\n"
done
shouldn't that be Shellchecks recommendation for arrays ?
Originally posted by @PeterPitterling in https://github.com/koalaman/shellcheck/issues/2374#issuecomment-969056887
This workaround is only required on Bash 3 and below. On Bash 4+, for item in "${array[@]}" will not trigger set -u when the array is empty.
ShellCheck currently does not differentiate between shell versions, so there's no way to indicate that you want Bash 3 support and check accordingly.
Just verified with bash 4.3.48 (SLES12.3) - still triggers "unbound variable"
for item in "${array[@]}"
array[@]: unbound variable
You're right, this was fixed in bash 4.4+ and not bash 4.0+:
- New Features in Bash
a. Using ${a[@]} or ${a[*]} with an array without any assigned elements when
the nounset option is enabled no longer throws an unbound variable error.
Still, with bash 5.2.15, getting the length of an empty array fails for unbound variable: ${#a[*]}" . Of course, this is just another "missing feature" in bash just like using ${a[@]} was.
Edit: but declaring the array like this declare -A a=(), instead of declare -A a, makes also ${#a[*]}" work. Did not expect this, because according to the chats in internet, this had not had any impact for the unbound variable error in case of ${a[@]}.
What's the context? Also, there's a bug submission form among ShellCheck's GitHub pages. Using that would be helpful. Wiley
On Wed, Dec 18, 2024, 21:15 karniemi @.***> wrote:
Still, with bash 5.2.15, getting the length of an empty array fails for unbound variable: ${#a[*]}" . Of course, this is just another "missing feature" in bash just like using ${a[@]} was.
— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/2387#issuecomment-2552804437, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F227QRNEOWA3XXFNLBL2GJI53AVCNFSM6AAAAABT4EX7FCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKNJSHAYDINBTG4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>
Nevermind. I saw the thread. W.
On Wed, Dec 25, 2024, 23:12 Wiley Young @.***> wrote:
What's the context? Also, there's a bug submission form among ShellCheck's GitHub pages. Using that would be helpful. Wiley
On Wed, Dec 18, 2024, 21:15 karniemi @.***> wrote:
Still, with bash 5.2.15, getting the length of an empty array fails for unbound variable: ${#a[*]}" . Of course, this is just another "missing feature" in bash just like using ${a[@]} was.
— Reply to this email directly, view it on GitHub https://github.com/koalaman/shellcheck/issues/2387#issuecomment-2552804437, or unsubscribe https://github.com/notifications/unsubscribe-auth/AUF2F227QRNEOWA3XXFNLBL2GJI53AVCNFSM6AAAAABT4EX7FCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKNJSHAYDINBTG4 . You are receiving this because you are subscribed to this thread.Message ID: @.***>