I think with the space, it interpreted that first line as a comment,
and used the default shell syntax to try to interpret the script
(Bourne maybe?). It worked from the terminal, I assume, because you
were executing it from withing the bash shell. But that level of
knowledge is somewhat out of my league. Glad you got it working.