What’s great is that Robert Shaw wrote that whole speech. He didn’t like what they had in the script so he wrote his own. Brilliant!
Yes he did write it. It is what made the movie and made his death more poignant.
He came back the next day, apologizing profusely, and asked for another chance, nailing the entire scene in one take.
The guy had more charisma than Pitt, Affleck, and Damon and a half dozen other of today's biggest box office stars combined.
And did it drunk, which I've heard he was for a good bit of the filming. It just gives his character more authenticity.