Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does adding & at the end of a command in bash do?
easy
A. Runs the command in the background, freeing the terminal immediately
B. Stops the command from running
C. Runs the command with higher priority
D. Runs the command only if another command fails

Solution

  1. Step 1: Understand the role of & in bash

    The & symbol placed after a command tells bash to run that command in the background.
  2. Step 2: Effect on terminal usability

    Running a command in the background frees the terminal immediately so you can type new commands without waiting.
  3. Final Answer:

    Runs the command in the background, freeing the terminal immediately -> Option A
  4. Quick Check:

    Background run = frees terminal [OK]
Hint: Remember: & means run in background and free terminal [OK]
Common Mistakes:
  • Thinking & stops the command
  • Confusing & with command priority
  • Assuming & waits for command to finish
2. Which of the following is the correct syntax to run sleep 10 in the background?
easy
A. sleep 10 &&
B. sleep 10 &
C. sleep & 10
D. & sleep 10

Solution

  1. Step 1: Identify correct placement of &

    The & must come immediately after the full command to run it in the background.
  2. Step 2: Check each option

    sleep 10 & places & correctly after sleep 10. The other options misuse & or use wrong symbols.
  3. Final Answer:

    sleep 10 & -> Option B
  4. Quick Check:

    Command & = correct syntax [OK]
Hint: Put & at the end of the command line [OK]
Common Mistakes:
  • Placing & before the command
  • Inserting & between command and arguments
  • Using && instead of &
3. What will be the output of the following commands?
echo "Start"
sleep 3 &
echo "End"
medium
A. Only Start is printed
B. Start\n(wait 3 seconds)\nEnd
C. Start\nEnd (immediately), then sleep runs silently
D. Start\nEnd\nError message

Solution

  1. Step 1: Analyze command order and background execution

    The sleep 3 & runs in the background, so it does not block the terminal.
  2. Step 2: Understand output timing

    "Start" prints first, then sleep 3 & starts silently in background, then "End" prints immediately.
  3. Final Answer:

    Start\nEnd (immediately), then sleep runs silently -> Option C
  4. Quick Check:

    Background sleep = immediate End print [OK]
Hint: Background commands don't block output [OK]
Common Mistakes:
  • Expecting sleep to delay 'End' output
  • Thinking background commands print output
  • Confusing & with && causing wait
4. Identify the error in this script snippet:
echo "Start"
sleep 5 &
wait
wait
 echo "Done"
medium
A. The echo command should be before wait
B. Using two wait commands causes an error
C. Missing & after sleep 5
D. The second wait is redundant but not an error

Solution

  1. Step 1: Understand wait behavior

    The wait command waits for all background jobs to finish. Multiple wait commands do not cause errors but the second is unnecessary.
  2. Step 2: Check script correctness

    The script runs sleep 5 in background, waits twice, then prints "Done". This works but the second wait is redundant.
  3. Final Answer:

    The second wait is redundant but not an error -> Option D
  4. Quick Check:

    Multiple waits allowed, redundant but no error [OK]
Hint: Multiple waits don't error, just wait again [OK]
Common Mistakes:
  • Thinking multiple waits cause errors
  • Forgetting & after sleep
  • Misplacing echo before wait
5. You want to run three commands cmd1, cmd2, and cmd3 in the background simultaneously, then wait for all to finish before printing "All done". Which script correctly does this?
hard
A. cmd1 & cmd2 & cmd3 & wait echo "All done"
B. cmd1 & cmd2 & cmd3 & wait & echo "All done"
C. cmd1 & cmd2 & cmd3 echo "All done"
D. cmd1 & cmd2 & cmd3 & echo "All done"

Solution

  1. Step 1: Run each command in background separately

    Each command must end with & to run in background independently.
  2. Step 2: Use wait to pause until all background jobs finish

    After starting all commands, wait pauses script until all finish, then prints "All done".
  3. Final Answer:

    cmd1 & cmd2 & cmd3 & wait echo "All done" -> Option A
  4. Quick Check:

    Separate & + wait before echo = correct [OK]
Hint: Put & after each command, then wait once [OK]
Common Mistakes:
  • Running all commands on one line without separate &
  • Omitting wait causing early echo
  • Placing echo on same line as background commands
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard