Run bash command on jenkins pipeline

160,168

Solution 1

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

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

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

Solution 3

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

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

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'.

Share:
160,168
Yago Azedias
Author by

Yago Azedias

Hey, there! Software Developer, technology enthusiastic and innovation lover. Currently, I am a software developer with almost 5 years of experience in web development. Enthusiastic about agile methodology, experienced in building amazing applications.

Updated on November 13, 2020

Comments

  • Yago Azedias
    Yago Azedias over 3 years

    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.

  • Yago Azedias
    Yago Azedias almost 7 years
    The logo returns a error for the "+" WorkflowScript: 9: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 9, column 68. 2c-437a-b8a2-db013fe42f62\"" +
  • Jacob
    Jacob almost 7 years
    Can you please post the script? From what you've pasted, it looks like there are two quotes before the +, which is wrong.
  • Yago Azedias
    Yago Azedias over 6 years
    Have you test it? I made with a way that I set to run a file inside my git repository, with this I have sure that the code will be exec as bash code, instead a shell code.
  • Jake
    Jake over 6 years
    Yes, it successfully worked for me. I had a script that used bash specific syntax which was failing until I formatted it so the first line was not blank.
  • DenCowboy
    DenCowboy over 6 years
    @Jake Any idea how you gen use a bash-specific script in a stage? This part works for me too, but when I try to trigger a bash script it does not work for me.
  • forzagreen
    forzagreen over 6 years
    Not all Jenkins versions support bash step. However sh '''#!/bin/bash ... should also work.
  • Marcello Romani
    Marcello Romani over 5 years
    java.lang.NoSuchMethodError: No such DSL method 'bash' found among steps
  • Anuradha Fernando
    Anuradha Fernando over 5 years
    Thank you @Jacob I was having the same problem and your comment helped me to solve those!
  • Isaac Freeman
    Isaac Freeman over 4 years
    Don't use "\n" and "+", just use tripple quotes to do a multi-line string.
  • mirekphd
    mirekphd over 4 years
    For multi-line scripts or those run multiple times, I would create a new bash script file (starting from #!/bin/bash), and simply run it with: chmod +x './script.sh' && sh './script.sh'
  • red888
    red888 about 2 years
    You can also just use newline in the same string: sh '#!/bin/bash\n echo "$SHELL"'