I believe your problem is that the output from "bash -x" is sent to stderr while your script's output is being sent to stdout.
When you executed:
bash -x script.sh > output.txt
...the normal output from your script was sent to the redirected file and the script commands (from the -x) continued to be displayed on your terminal screen.
In order to send stderr and stdout to the same stream, you need to duplicate one atop the other. This is done using shell file descriptor dupe syntax "e.g. 2>&1"), like so:
bash -x script.sh > output.txt 2>&1
This syntax duplicates file descriptor #2 (stderr) over the top of file descriptor #1 (stdout)... which results in both streams getting redirected to your output file.