0
0
Bash Scriptingscripting~10 mins

Running commands in background (&) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Running commands in background (&)
Start command
Add & to command
Shell runs command in background
Shell prompt returns immediately
Command runs independently
User can run other commands
The shell runs the command in the background when & is added, so the prompt returns immediately and the command runs independently.
Execution Sample
Bash Scripting
sleep 5 &
echo "Done"
Runs 'sleep 5' in the background, then immediately prints 'Done'.
Execution Table
StepCommandActionShell PromptOutput
1sleep 5 &Starts 'sleep 5' in backgroundReturns immediately
2echo "Done"Runs immediately afterPrompt returns immediatelyDone
3sleep 5 completesBackground job finishesPrompt ready for next command
💡 After 'sleep 5' finishes in background, shell is ready for new commands.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
Background JobNonesleep 5 runningsleep 5 runningNone (finished)
Shell PromptWaitingReturned immediatelyReturned immediatelyWaiting
Key Moments - 3 Insights
Why does the shell prompt return immediately after running 'sleep 5 &'?
Because the & tells the shell to run the command in the background, so it does not wait for it to finish before showing the prompt again, as shown in execution_table step 1.
Does the 'echo "Done"' command wait for 'sleep 5' to finish?
No, 'echo "Done"' runs immediately after starting 'sleep 5' in background, as shown in execution_table step 2.
What happens to the background job after it finishes?
The background job ends and frees resources, and the shell prompt remains ready for new commands, as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the shell prompt state immediately after running 'sleep 5 &'?
ADisappears
BReturns immediately
CWaits for sleep to finish
DShows an error
💡 Hint
Check execution_table row 1, column 'Shell Prompt'
At which step does the 'echo "Done"' command run?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
Look at execution_table rows and see when 'echo "Done"' runs
If we remove '&' from 'sleep 5 &', how would the shell prompt behave?
AWaits 5 seconds before returning
BReturns immediately as before
CRuns sleep in background anyway
DShows an error
💡 Hint
Without '&', shell waits for command to finish before prompt returns
Concept Snapshot
Running commands in background (&):
- Add & at end of command
- Shell runs command without waiting
- Prompt returns immediately
- Command runs independently
- Useful to do multiple tasks at once
Full Transcript
When you add an ampersand (&) at the end of a command in bash, the shell runs that command in the background. This means the shell does not wait for the command to finish. Instead, it immediately shows the prompt so you can type other commands. For example, 'sleep 5 &' starts a 5-second pause in the background. Right after, you can run 'echo "Done"' which prints 'Done' right away. The sleep command finishes quietly in the background after 5 seconds. This lets you do other work while waiting.