Bird
Raised Fist0
Linux CLIscripting~10 mins

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

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

Practice

(1/5)
1. What does the fg command do in a Linux shell?
easy
A. Brings a background job to the foreground for interaction
B. Stops a running job immediately
C. Lists all running jobs
D. Terminates a background job

Solution

  1. Step 1: Understand the purpose of fg

    The fg command is used to bring a job running in the background to the foreground so you can interact with it directly.
  2. Step 2: Compare with other commands

    jobs lists jobs, bg resumes jobs in background, and stopping or terminating jobs use other commands.
  3. Final Answer:

    Brings a background job to the foreground for interaction -> Option A
  4. Quick Check:

    fg = bring job to foreground [OK]
Hint: Remember: fg = foreground, bg = background [OK]
Common Mistakes:
  • Confusing fg with bg
  • Thinking fg lists jobs
  • Assuming fg stops jobs
2. Which of the following is the correct syntax to resume a stopped job in the background?
easy
A. fg %1
B. bg %1
C. jobs %1
D. stop %1

Solution

  1. Step 1: Identify the command to resume a stopped job in background

    The bg command resumes a stopped job in the background, allowing the terminal to be free.
  2. Step 2: Understand job specification

    Using %1 specifies job number 1. So bg %1 resumes job 1 in background.
  3. Final Answer:

    bg %1 -> Option B
  4. Quick Check:

    bg %jobnumber resumes job in background [OK]
Hint: Use bg with %jobnumber to resume in background [OK]
Common Mistakes:
  • Using fg instead of bg to resume in background
  • Trying to use jobs command to resume
  • Using stop which is invalid
3. Given the following commands run in a terminal:
sleep 100 &
jobs
fg %1

What will happen after running fg %1?
medium
A. The sleep command will continue running in the background
B. An error will occur because %1 is invalid
C. The sleep command will be terminated
D. The sleep command will be brought to the foreground and block the terminal

Solution

  1. Step 1: Analyze the commands

    sleep 100 & runs sleep in background. jobs lists it as job 1. fg %1 brings job 1 to foreground.
  2. Step 2: Understand effect of fg

    Bringing sleep to foreground means it blocks terminal until it finishes or is stopped.
  3. Final Answer:

    The sleep command will be brought to the foreground and block the terminal -> Option D
  4. Quick Check:

    fg %1 = foreground job blocks terminal [OK]
Hint: fg brings job to foreground and blocks terminal [OK]
Common Mistakes:
  • Thinking fg keeps job in background
  • Assuming fg terminates job
  • Believing %1 is invalid without jobs output
4. You ran a command in the background with sleep 200 & but accidentally typed fg 1 instead of fg %1. What will happen?
medium
A. The job will be brought to foreground successfully
B. The job will continue running in background silently
C. An error message will appear: 'fg: 1: no such job'
D. The shell will terminate

Solution

  1. Step 1: Understand job specification syntax

    The fg command requires job IDs to be prefixed with %, e.g., fg %1.
  2. Step 2: Analyze incorrect command

    Typing fg 1 without % causes shell to look for a job named '1' which does not exist, causing an error.
  3. Final Answer:

    An error message will appear: 'fg: 1: no such job' -> Option C
  4. Quick Check:

    Job IDs need % prefix for fg/bg [OK]
Hint: Always prefix job number with % in fg/bg commands [OK]
Common Mistakes:
  • Omitting % before job number
  • Assuming fg accepts plain numbers
  • Ignoring error messages
5. You have two stopped jobs listed by jobs as:
[1]+  Stopped                 nano file1.txt
[2]-  Stopped                 vim file2.txt

How do you resume vim file2.txt in the background and then bring nano file1.txt to the foreground?
hard
A. bg %2 followed by fg %1
B. fg %2 followed by bg %1
C. bg %1 followed by fg %2
D. fg %1 followed by fg %2

Solution

  1. Step 1: Resume vim in background

    Use bg %2 to resume job 2 (vim) in background so it runs without blocking terminal.
  2. Step 2: Bring nano to foreground

    Use fg %1 to bring job 1 (nano) to foreground for interaction.
  3. Final Answer:

    bg %2 followed by fg %1 -> Option A
  4. Quick Check:

    bg %job runs in background, fg %job runs in foreground [OK]
Hint: Use bg for background, fg for foreground with correct job IDs [OK]
Common Mistakes:
  • Mixing up job numbers
  • Using fg for both jobs
  • Not using % before job number
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