I am now able to recreate this from the command line. If I have the script call /bin/bash instead of what I want to type and then I run the script (from command line), and then I run the cgi manually, it fails in the same manner as CGI.
The user is correct (www-data) but there are no environment variables set..?