Timeline for answer to Check if array is empty in Bash by x-yuri
Current License: CC BY-SA 4.0
Post Revisions
6 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 21, 2023 at 16:31 | comment | added | Brian Thomas | ok yeah, see that one here that @dimo414 mentioned stackoverflow.com/a/65957757/1091853 | |
| Apr 14, 2023 at 6:24 | comment | added | dimo414 |
This is the correct answer. You can also write it as (( ${#a[@]} != 0 )) if you prefer to make the non-zero check explicit.
|
|
| Dec 15, 2021 at 4:09 | comment | added | Brandon |
@x-yuri it works for me: if I run that exact command, the output is 5.0.17(1)-release / not empty
|
|
| Dec 17, 2020 at 21:38 | comment | added | x-yuri |
It might have been so once, but I can't reproduce it. If echo $BASH_VERSION && a=('') && (( ${#a[@]} )) && echo not empty || echo empty happens to output "empty," fell free to tell your version.
|
|
| Aug 15, 2018 at 12:36 | comment | added | cxw |
Nice and clean! I like it. I also note that if the first element of the array is always nonempty, (( ${#a} )) (length of the first element) will work also. However, that will fail on a=(''), whereas (( ${#a[@]} )) given in the answer will succeed.
|
|
| Aug 2, 2018 at 6:04 | history | answered | x-yuri | CC BY-SA 4.0 |