Bird
Raised Fist0
Jenkinsdevops~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In Jenkins pipelines, which shell step should you use to run a command on a Linux agent?
easy
A. bat
B. sh
C. cmd
D. powershell

Solution

  1. Step 1: Understand Jenkins shell steps

    Jenkins uses sh to run shell commands on Linux/macOS agents.
  2. Step 2: Identify the correct shell for Linux

    Since Linux uses bash or sh shells, sh is the correct step.
  3. Final Answer:

    sh -> Option B
  4. Quick Check:

    Linux shell step = sh [OK]
Hint: Use sh for Linux/macOS, bat for Windows agents [OK]
Common Mistakes:
  • Using bat on Linux agents
  • Confusing cmd with sh
  • Using powershell on non-Windows agents
2. Which of the following is the correct syntax to run a Windows batch command in a Jenkins pipeline?
easy
A. sh 'dir'
B. cmd 'dir'
C. powershell 'dir'
D. bat 'dir'

Solution

  1. Step 1: Identify Windows shell step in Jenkins

    Jenkins uses bat to run Windows batch commands.
  2. Step 2: Match syntax for batch command

    The correct syntax is bat 'command', so bat 'dir' is correct.
  3. Final Answer:

    bat 'dir' -> Option D
  4. Quick Check:

    Windows batch command = bat [OK]
Hint: Use bat for Windows commands in Jenkins pipelines [OK]
Common Mistakes:
  • Using sh for Windows commands
  • Using cmd which is not a Jenkins step
  • Using powershell without proper syntax
3. What will be the output of this Jenkins pipeline snippet on a Linux agent?
sh 'echo Hello Jenkins'
medium
A. Hello Jenkins
B. sh: command not found
C. echo Hello Jenkins
D. Syntax error

Solution

  1. Step 1: Understand the sh step execution

    The sh step runs the command inside quotes on the Linux shell.
  2. Step 2: Predict command output

    The command echo Hello Jenkins prints the text 'Hello Jenkins' to the console.
  3. Final Answer:

    Hello Jenkins -> Option A
  4. Quick Check:

    sh echo outputs text [OK]
Hint: sh runs commands and prints their output on Linux [OK]
Common Mistakes:
  • Expecting the command itself as output
  • Confusing syntax error with correct command
  • Assuming sh step fails on Linux
4. You wrote this Jenkins pipeline step:
bat 'echo Hello from Linux'
But it runs on a Linux agent and fails. What is the problem?
medium
A. bat is for Windows, not Linux
B. echo command is invalid
C. Missing quotes around command
D. Pipeline syntax error

Solution

  1. Step 1: Identify shell step and agent OS mismatch

    The bat step runs Windows batch commands, but the agent is Linux.
  2. Step 2: Understand why failure occurs

    Linux agents do not support bat, so the step fails.
  3. Final Answer:

    bat is for Windows, not Linux -> Option A
  4. Quick Check:

    bat step on Linux = failure [OK]
Hint: Match shell step to agent OS: bat=Windows, sh=Linux [OK]
Common Mistakes:
  • Thinking echo is invalid on Linux
  • Assuming quotes cause error
  • Blaming pipeline syntax instead of shell step
5. You want to write a Jenkins pipeline that runs a shell command on both Windows and Linux agents. Which approach is best?
hard
A. Use sh step for all agents
B. Use bat step for all agents
C. Use sh for Linux and bat for Windows with if condition
D. Use powershell step for all agents

Solution

  1. Step 1: Recognize OS differences in shell steps

    Linux agents require sh, Windows agents require bat.
  2. Step 2: Use conditional logic in pipeline

    Use an if condition to detect OS and run the correct shell step.
  3. Final Answer:

    Use sh for Linux and bat for Windows with if condition -> Option C
  4. Quick Check:

    Conditional shell steps match OS [OK]
Hint: Use if condition to select sh or bat by OS [OK]
Common Mistakes:
  • Using one shell step for all OSes
  • Ignoring agent OS detection
  • Using powershell without Windows-only context
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard