@nikolalThanks, it seems to work well in Bash now. My mistake was not adding permission to execute script, removing sudo from commands and then just executing whole script as sudo. Just need to get the random hostname generation ironed out now.