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 for programmers to stay up-to-date, learn new skills, and share ideas.
We’ll never post without your permission.