--Added double quotes around various variables to avoid globbing and word splitting.
--Removed use of legacy basetick and replaced with $()
--Avoid use of -a and -o and replaced with || and && which are more preferred.
Suggestions and fixes were recommended by shellcheck.