Run bash command on jenkins pipeline
The Groovy script you provided is formatting the first line as a blank line in the resultant script. The shebang, telling the script to run with /bin/bash instead of /bin/sh, needs to be on the first line of the file or it will be ignored.
So instead, you should format your Groovy like this:
stage('Setting the variables values') { steps { sh '''#!/bin/bash echo "hello world" ''' }}
And it will execute with /bin/bash.
I'm sure that the above answers work perfectly. However, I had the difficulty of adding the double quotes as my bash lines where closer to 100. So, the following way helped me. (In a nutshell, no double quotes around each line of the shell)
Also, when I had "bash '''#!/bin/bash" within steps, I got the following error java.lang.NoSuchMethodError: No such DSL method '**bash**' found among steps
pipeline { agent none stages { stage ('Hello') { agent any steps { echo 'Hello, ' sh '''#!/bin/bash echo "Hello from bash" echo "Who I'm $SHELL" ''' } } }}
The result of the above execution is