0
0
Jenkinsdevops~10 mins

Shell steps (sh, bat) in Jenkins - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Shell steps (sh, bat)
Start Jenkins Pipeline
Execute shell step
Run command in shell
Capture output and status
Use output or handle errors
Continue or stop pipeline
The Jenkins pipeline runs a shell step, executes the command, captures output and status, then continues or stops based on results.
Execution Sample
Jenkins
pipeline {
  agent any
  stages {
    stage('Shell Step') {
      steps {
        sh 'echo Hello Jenkins'
      }
    }
  }
}
This pipeline runs a shell command that prints 'Hello Jenkins' to the console.
Process Table
StepActionCommand ExecutedOutputStatus
1Start pipeline--Running
2Enter stage 'Shell Step'--Running
3Execute shell stepecho Hello JenkinsHello JenkinsSuccess
4Shell step completed--Success
5Pipeline continues or ends--Success
💡 Shell command executed successfully, pipeline continues.
Status Tracker
VariableStartAfter Step 3Final
outputnullHello JenkinsHello Jenkins
statusnullSuccessSuccess
Key Moments - 2 Insights
Why does the pipeline stop if the shell command fails?
Because Jenkins treats a non-zero exit code from the shell step as a failure, stopping the pipeline as shown in step 3 if status is not 'Success'.
How can I capture the output of the shell command for later use?
You can assign the output to a variable using 'def output = sh(script: "command", returnStdout: true).trim()' to use it later, unlike the simple 'sh' step shown in the table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after step 3?
Aecho Hello Jenkins
BSuccess
CHello Jenkins
D-
💡 Hint
Check the 'Output' column in row for step 3.
At which step does Jenkins run the shell command?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Command Executed' columns to find when the command runs.
If the shell command returned an error, what would happen to the pipeline status at step 4?
AStatus would be 'Success'
BStatus would be 'Failure'
CStatus would be 'Running'
DStatus would be '-'
💡 Hint
Refer to the explanation in key moments about non-zero exit codes stopping the pipeline.
Concept Snapshot
Jenkins shell steps run commands in a shell environment.
Use 'sh' for Linux/macOS and 'bat' for Windows.
Commands run inside pipeline stages.
Output and status are captured.
Non-zero exit codes fail the pipeline.
Use 'returnStdout: true' to capture output.
Full Transcript
In Jenkins pipelines, shell steps like 'sh' run commands in a shell environment. The pipeline starts, enters a stage, executes the shell command, captures its output and status, then continues or stops based on success or failure. For example, running 'sh "echo Hello Jenkins"' prints 'Hello Jenkins' and returns success. If the command fails, Jenkins stops the pipeline. You can capture command output by using 'returnStdout: true' in the shell step. This visual trace shows each step from starting the pipeline to executing the shell command and handling the result.