Running commands in background (&) in Bash Scripting - Time & Space Complexity
When we run commands in the background using &, we want to know how this affects the total time our script takes.
We ask: How does running tasks in the background change the time needed as we add more commands?
Analyze the time complexity of the following code snippet.
for i in {1..n}; do
sleep 1 &
done
wait
This script starts n sleep commands in the background, each pausing for 1 second, then waits for all to finish.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Starting
nbackground sleep commands. - How many times: The loop runs
ntimes, launching all commands almost instantly.
Even though we start n commands, they run at the same time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 second total |
| 100 | About 1 second total |
| 1000 | About 1 second total |
Pattern observation: Total time stays roughly the same no matter how many commands run in background.
Time Complexity: O(1)
This means the total time does not grow with more commands because they run at the same time.
[X] Wrong: "Running more background commands always takes more total time."
[OK] Correct: Since commands run simultaneously, total time depends on the longest single command, not the number of commands.
Understanding how background tasks affect time helps you write scripts that save time and run efficiently, a useful skill in many real-world tasks.
What if the commands run in background have different durations? How would the total time change?
Practice
& at the end of a command in bash do?Solution
Step 1: Understand the role of
The&in bash&symbol placed after a command tells bash to run that command in the background.Step 2: Effect on terminal usability
Running a command in the background frees the terminal immediately so you can type new commands without waiting.Final Answer:
Runs the command in the background, freeing the terminal immediately -> Option AQuick Check:
Background run = frees terminal [OK]
- Thinking & stops the command
- Confusing & with command priority
- Assuming & waits for command to finish
sleep 10 in the background?Solution
Step 1: Identify correct placement of
The&&must come immediately after the full command to run it in the background.Step 2: Check each option
sleep 10 & places&correctly aftersleep 10. The other options misuse&or use wrong symbols.Final Answer:
sleep 10 & -> Option BQuick Check:
Command & = correct syntax [OK]
- Placing & before the command
- Inserting & between command and arguments
- Using && instead of &
echo "Start" sleep 3 & echo "End"
Solution
Step 1: Analyze command order and background execution
Thesleep 3 &runs in the background, so it does not block the terminal.Step 2: Understand output timing
"Start" prints first, thensleep 3 &starts silently in background, then "End" prints immediately.Final Answer:
Start\nEnd (immediately), then sleep runs silently -> Option CQuick Check:
Background sleep = immediate End print [OK]
- Expecting sleep to delay 'End' output
- Thinking background commands print output
- Confusing & with && causing wait
echo "Start" sleep 5 & wait wait echo "Done"
Solution
Step 1: Understand
Thewaitbehaviorwaitcommand waits for all background jobs to finish. Multiplewaitcommands do not cause errors but the second is unnecessary.Step 2: Check script correctness
The script runssleep 5in background, waits twice, then prints "Done". This works but the secondwaitis redundant.Final Answer:
The second wait is redundant but not an error -> Option DQuick Check:
Multiple waits allowed, redundant but no error [OK]
- Thinking multiple waits cause errors
- Forgetting & after sleep
- Misplacing echo before wait
cmd1, cmd2, and cmd3 in the background simultaneously, then wait for all to finish before printing "All done". Which script correctly does this?Solution
Step 1: Run each command in background separately
Each command must end with&to run in background independently.Step 2: Use
After starting all commands,waitto pause until all background jobs finishwaitpauses script until all finish, then prints "All done".Final Answer:
cmd1 & cmd2 & cmd3 & wait echo "All done" -> Option AQuick Check:
Separate & + wait before echo = correct [OK]
- Running all commands on one line without separate &
- Omitting wait causing early echo
- Placing echo on same line as background commands
Ready to prove it?
Pass the test and this pattern moves unknown
