Shell steps (sh, bat) in Jenkins - Time & Space Complexity
We want to understand how the time taken by shell steps in Jenkins changes as the commands run more or process more data.
Specifically, how does running shell commands inside Jenkins scale with input size?
Analyze the time complexity of the following Jenkins pipeline snippet using shell steps.
pipeline {
agent any
stages {
stage('Run Shell Commands') {
steps {
sh 'for i in $(seq 1 100); do echo $i; done'
}
}
}
}
This code runs a shell loop printing numbers from 1 to 100 inside a Jenkins pipeline.
Look for repeated actions inside the shell step.
- Primary operation: The shell loop runs the echo command multiple times.
- How many times: The loop runs 100 times, once for each number.
The number of echo commands grows directly with the loop count.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 echo commands |
| 100 | 100 echo commands |
| 1000 | 1000 echo commands |
Pattern observation: The total work grows linearly as the number of loop iterations increases.
Time Complexity: O(n)
This means the time taken grows in direct proportion to the number of commands run inside the shell loop.
[X] Wrong: "Running shell steps inside Jenkins always takes constant time regardless of commands."
[OK] Correct: The time depends on what the shell commands do and how many times they run, so more commands mean more time.
Understanding how shell steps scale helps you explain pipeline performance and troubleshoot slow builds confidently.
"What if the shell loop runs nested loops inside? How would the time complexity change?"
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
