Excellent advice all around.
shellcheck is a winner.
I'd also add, split your script into functions and do most of the work inside "main" or similar func. That makes it cleaner which parts of the code are synchronously executed and which are functionality to be called later (an alternative to the subshell () advice above).
We’re a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.