What if you never had to type the same commands again and still get perfect results every time?
Why Shell steps (sh, bat) in Jenkins? - Purpose & Use Cases
Imagine you have to run a series of commands on different computers manually, typing each command one by one in a terminal or command prompt.
You need to do this every time you want to build or test your software.
This manual way is slow and boring.
You might forget a command or make a typo.
It's hard to repeat the exact same steps every time, and mistakes can cause big problems.
Shell steps in Jenkins let you write those commands once in a script.
Jenkins runs them automatically on the right machine, every time, without mistakes.
This saves time and makes your work reliable.
Open terminal Type: git pull Type: ./build.sh Type: ./test.sh
sh 'git pull' sh './build.sh' sh './test.sh'
You can automate complex tasks easily and run them anytime with one click.
A developer pushes code to a shared repository.
Jenkins automatically runs shell steps to build the app and run tests without anyone typing commands.
Manual command typing is slow and error-prone.
Shell steps automate running commands in Jenkins.
This makes builds and tests fast, reliable, and repeatable.
Practice
Solution
Step 1: Understand Jenkins shell steps
Jenkins usesshto run shell commands on Linux/macOS agents.Step 2: Identify the correct shell for Linux
Since Linux uses bash or sh shells,shis the correct step.Final Answer:
sh-> Option BQuick Check:
Linux shell step = sh [OK]
- Using bat on Linux agents
- Confusing cmd with sh
- Using powershell on non-Windows agents
Solution
Step 1: Identify Windows shell step in Jenkins
Jenkins usesbatto run Windows batch commands.Step 2: Match syntax for batch command
The correct syntax isbat 'command', sobat 'dir'is correct.Final Answer:
bat 'dir' -> Option DQuick Check:
Windows batch command = bat [OK]
- Using sh for Windows commands
- Using cmd which is not a Jenkins step
- Using powershell without proper syntax
sh 'echo Hello Jenkins'
Solution
Step 1: Understand the sh step execution
Theshstep runs the command inside quotes on the Linux shell.Step 2: Predict command output
The commandecho Hello Jenkinsprints the text 'Hello Jenkins' to the console.Final Answer:
Hello Jenkins -> Option AQuick Check:
sh echo outputs text [OK]
- Expecting the command itself as output
- Confusing syntax error with correct command
- Assuming sh step fails on Linux
bat 'echo Hello from Linux'But it runs on a Linux agent and fails. What is the problem?
Solution
Step 1: Identify shell step and agent OS mismatch
Thebatstep runs Windows batch commands, but the agent is Linux.Step 2: Understand why failure occurs
Linux agents do not supportbat, so the step fails.Final Answer:
bat is for Windows, not Linux -> Option AQuick Check:
bat step on Linux = failure [OK]
- Thinking echo is invalid on Linux
- Assuming quotes cause error
- Blaming pipeline syntax instead of shell step
Solution
Step 1: Recognize OS differences in shell steps
Linux agents requiresh, Windows agents requirebat.Step 2: Use conditional logic in pipeline
Use anifcondition to detect OS and run the correct shell step.Final Answer:
Use sh for Linux and bat for Windows with if condition -> Option CQuick Check:
Conditional shell steps match OS [OK]
- Using one shell step for all OSes
- Ignoring agent OS detection
- Using powershell without Windows-only context
Ready to prove it?
Pass the test and this pattern moves unknown
