You're trying to change the contents of /opt/Folder to your own userid, aren't you.
Here's your command once again:
sudo chown -R $USER:$USER /opt/Folder
This works from the command line because the $USER variable is evaluated before the command is executed, and it is therefore executed as this (assuming your username is george):
sudo chown -R george:george /opt/Folder
On the other hand, when you run a command with sudo, amongst other things the $USER variable takes the value of the target user, which in this case is root. So by running sudo bash mysetup.sh you are running mysetup.sh as root, so the command runs with $USER set to root and is evaluated like this
sudo chown -R root:root /opt/Folder
The solution in your case is to avoid running the entire script itself under sudo, and let the script use sudo where necessary to run specific commands with elevated privileges. In other words, use bash mysetup.sh instead of sudo bash mysetup.sh.
     
    
/bin/bash -xit to trace what is going on?/bin/bash -xfrom a terminal?It shows many lines and I can't see something related.I tried also#! /bin/bash -xinside the script but where do I see the messages?bash -x scriptname. Or putset -xin the script itself.