📖 Scenario: You are setting up a Jenkins pipeline to automate simple shell commands. This will help you understand how to run shell scripts inside Jenkins jobs.
🎯 Goal: Build a Jenkins pipeline script that runs shell commands using sh steps on Linux or macOS, and bat steps on Windows agents.
📋 What You'll Learn
Create a Jenkins pipeline script with a pipeline block
Use a stage named Setup to run shell commands
Use sh step to run a Linux/macOS shell command
Use bat step to run a Windows batch command
Print the output of the commands in the Jenkins console
💡 Why This Matters
🌍 Real World
Jenkins pipelines automate software builds and deployments by running shell commands on build agents.
💼 Career
Knowing how to use <code>sh</code> and <code>bat</code> steps is essential for DevOps engineers to automate tasks across different operating systems.
Progress0 / 4 steps
1
Create a Jenkins pipeline with a pipeline block
Write a Jenkins pipeline script starting with a pipeline block and an empty agent any section.
Jenkins
Hint
Start with pipeline { and add agent any inside it.
2
Add a stage named Setup
Inside the pipeline block, add a stages block with one stage named Setup.
Jenkins
Hint
Add stages {} and inside it a stage('Setup') { steps { } } block.
3
Add a sh step to run echo Hello from Linux
Inside the steps block of the Setup stage, add a sh step that runs the command echo Hello from Linux.
Jenkins
Hint
Use sh 'echo Hello from Linux' inside steps {}.
4
Add a bat step to run echo Hello from Windows
Below the sh step inside the steps block, add a bat step that runs the command echo Hello from Windows.
Jenkins
Hint
Add bat 'echo Hello from Windows' below the sh step.
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
Step 1: Understand Jenkins shell steps
Jenkins uses sh to run shell commands on Linux/macOS agents.
Step 2: Identify the correct shell for Linux
Since Linux uses bash or sh shells, sh is the correct step.
Final Answer:
sh -> Option B
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
Step 1: Identify Windows shell step in Jenkins
Jenkins uses bat to run Windows batch commands.
Step 2: Match syntax for batch command
The correct syntax is bat 'command', so bat 'dir' is correct.
Final Answer:
bat 'dir' -> Option D
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
Step 1: Understand the sh step execution
The sh step runs the command inside quotes on the Linux shell.
Step 2: Predict command output
The command echo Hello Jenkins prints the text 'Hello Jenkins' to the console.
Final Answer:
Hello Jenkins -> Option A
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
Step 1: Identify shell step and agent OS mismatch
The bat step runs Windows batch commands, but the agent is Linux.
Step 2: Understand why failure occurs
Linux agents do not support bat, so the step fails.
Final Answer:
bat is for Windows, not Linux -> Option A
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
Step 1: Recognize OS differences in shell steps
Linux agents require sh, Windows agents require bat.
Step 2: Use conditional logic in pipeline
Use an if condition to detect OS and run the correct shell step.
Final Answer:
Use sh for Linux and bat for Windows with if condition -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.