Run bash command on jenkins pipeline

LinuxBashShellJenkinsGroovy

Linux Problem Overview


Inside a groovy script (for a jenkins pipeline): How can I run a bash command instead of a sh command?

I have tried the following:

Call "#!/bin/bash" inside the sh call:

stage('Setting the variables values') {
    steps {
         sh '''
            #!/bin/bash
            echo "hello world"
         '''
    }
}

Replace the sh call with a bash call:

stage('Setting the variables values') {
    steps {
         bash '''
            #!/bin/bash
            echo "hello world"
         '''
    }
}

Additional Info:

My command is more complex than a echo hello world.

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

According to this document, you should be able to do it like so:

node {
    sh "#!/bin/bash \n" + 
       "echo \"Hello from \$SHELL\""
}

Solution 3 - Linux

For multi-line shell scripts or those run multiple times, I would create a new bash script file (starting from #!/bin/bash), and simply run it with sh from Jenkinsfile:

sh 'chmod +x ./script.sh'
sh './script.sh'

Solution 4 - Linux

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

enter image description here

Solution 5 - Linux

If you want to change your default shell to bash for all projects on Jenkins, you can do so in the Jenkins config through the web portal:

Manage Jenkins > Configure System (Skip this clicking if you want by just going to https://{YOUR_JENKINS_URL}/configure.)

Fill in the field marked 'Shell executable' with the value /bin/bash and click 'Save'.

Solution 6 - Linux

In my case, I had to execute a Shell script in bash via jenkinsfile. Here's what worked for me :

sh 'core_devops/automation/scripts/ecs_initialize.sh'

And the first line in the script had

#!/bin/bash

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYago AzediasView Question on Stackoverflow
Solution 1 - LinuxJakeView Answer on Stackoverflow
Solution 2 - LinuxJacobView Answer on Stackoverflow
Solution 3 - LinuxmirekphdView Answer on Stackoverflow
Solution 4 - LinuxSantosh Kumar ArjunanView Answer on Stackoverflow
Solution 5 - LinuxJellicleCatView Answer on Stackoverflow
Solution 6 - LinuxiArc13View Answer on Stackoverflow