Problem
A groovy script (for a jenkins pipeline) might look like this: Is there a way to run a bash command rather than a sh command?
The following are some of the things I’ve tried:
Inside the sh call, type “#!/bin/bash”:
stage('Setting the variables values') {
steps {
sh '''
#!/bin/bash
echo "hello world"
'''
}
}
Substitute a bash command for the sh command:
stage('Setting the variables values') {
steps {
bash '''
#!/bin/bash
echo "hello world"
'''
}
}
Additional Info:
My command is a little more involved than an echo hello world.
Asked by Yago Azedias
Solution #1
In the final script, the Groovy script you provided formats the first line as a blank line. The shebang, which instructs the script to use /bin/bash rather than /bin/sh, must appear on the first line of the file or else it will be ignored.
As a result, you should format your Groovy as follows:
stage('Setting the variables values') {
steps {
sh '''#!/bin/bash
echo "hello world"
'''
}
}
And /bin/bash will be used to run it.
Answered by Jake
Solution #2
You should be able to do it as follows, according to this document:
node {
sh "#!/bin/bash \n" +
"echo \"Hello from \$SHELL\""
}
Answered by Jacob
Solution #3
I would create a new bash script file (beginning with #!/bin/bash) for multi-line shell scripts or ones that are run several times, and just run it with sh from Jenkinsfile:
sh 'chmod +x ./script.sh'
sh './script.sh'
Answered by mirekphd
Solution #4
I’m confident that the aforementioned responses are correct. However, because my bash lines were closer to 100, it was tough for me to include the double quotes. As a result, I found the following method to be helpful. (To summarize, there should be no double quotes around each line of the shell.)
I also received the java.lang problem when I had “bash “‘#!/bin/bash” within steps. NoSuchMethodError: There is no such DSL method ‘**bash**’ among the steps.
pipeline {
agent none
stages {
stage ('Hello') {
agent any
steps {
echo 'Hello, '
sh '''#!/bin/bash
echo "Hello from bash"
echo "Who I'm $SHELL"
'''
}
}
}
}
As a result of the preceding procedure,
Answered by Santosh Kumar Arjunan
Solution #5
If you wish to change your default shell for all Jenkins projects to bash, you can do so via the web interface in the Jenkins config:
Manage Jenkins > System Configuration (You can skip this step by visiting to https://YOUR JENKINS URL/configure.)
Click ‘Save’ after filling up the ‘Shell executable’ field with the value /bin/bash.
Answered by JellicleCat
Post is based on https://stackoverflow.com/questions/44330148/run-bash-command-on-jenkins-pipeline