0
0
Linux CLIscripting~10 mins

fg and bg commands in Linux CLI - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - fg and bg commands
Start a command in shell
Press Ctrl+Z to suspend
Job is stopped and moved to background
Use 'bg' to resume job in background
Job runs in background
Use 'fg' to bring job to foreground
Job runs in foreground
Job completes or suspended again
This flow shows how a running command can be suspended, then resumed in background with 'bg' or brought back to foreground with 'fg'.
Execution Sample
Linux CLI
sleep 100
# Press Ctrl+Z
bg
fg
Starts a sleep command, suspends it, resumes in background, then brings it back to foreground.
Execution Table
StepActionShell OutputJob StateForeground/Background
1Run 'sleep 100'No outputRunningForeground
2Press Ctrl+Z[1]+ Stopped sleep 100StoppedSuspended
3Run 'bg'[1]+ sleep 100 &RunningBackground
4Run 'fg'sleep 100RunningForeground
5Job completes or user interruptsNo outputDoneForeground
💡 Job finishes or is interrupted, shell prompt returns.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Job StateRunningStoppedRunningRunningDone
Foreground/BackgroundForegroundSuspendedBackgroundForegroundForeground
Key Moments - 3 Insights
Why does the job stop after pressing Ctrl+Z?
Pressing Ctrl+Z sends a stop signal to the job, suspending it (see Step 2 in execution_table). The shell marks it as stopped and frees the terminal.
What is the difference between 'bg' and 'fg'?
'bg' resumes the stopped job in the background allowing you to keep using the shell (Step 3). 'fg' brings the job back to the foreground so it can interact with the terminal (Step 4).
Can you run multiple jobs in background?
Yes, each job can be suspended and resumed independently with 'bg' or 'fg'. The shell tracks jobs by numbers (not shown here).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the job state after pressing Ctrl+Z?
AStopped
BRunning
CDone
DBackground
💡 Hint
Check Step 2 in execution_table under 'Job State'
At which step does the job run in the background?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at 'Foreground/Background' column in execution_table
If you skip 'bg' and run 'fg' immediately after Ctrl+Z, what happens?
AJob resumes in background
BJob stays stopped
CJob resumes in foreground
DShell shows error
💡 Hint
Recall 'fg' brings job to foreground regardless of background state (see Step 4)
Concept Snapshot
fg and bg commands manage job control in the shell.
Ctrl+Z suspends a running job (stops it).
'bg' resumes the job in background (no terminal control).
'fg' resumes the job in foreground (terminal control).
Use these to multitask in the shell.
Full Transcript
This lesson shows how to control jobs in a Linux shell using fg and bg commands. When you run a command like 'sleep 100', it runs in the foreground. Pressing Ctrl+Z suspends the job, stopping it and freeing the terminal. The shell marks the job as stopped. You can then use 'bg' to resume the job in the background, letting you keep using the shell prompt. Alternatively, 'fg' brings the job back to the foreground so it can interact with the terminal again. The execution table traces these steps, showing job states and whether the job runs in foreground or background. Key moments clarify why Ctrl+Z stops the job, the difference between fg and bg, and that multiple jobs can be managed this way. The visual quiz tests understanding of job states and command effects. The quick snapshot summarizes the commands and their use for job control.