Ok I think I see the issue
It's lines, lines and more lines of bash script
sigh
big sigh
Using bash for all this stuff is like trying to wash your car with sandpaper instead of soap and water. Yes it can work if you're really careful with it, but in practice no