Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

fg Command Guide for Bringing Jobs to the Foreground

fg takes a stopped or background job and moves it back to the foreground, where you can see it and interact with it again.


What is fg, and Why Use It?

The fg command resumes a job in the foreground — the terminal you are typing in. Use it when you started a job with &, suspended it with Ctrl-Z, or backgrounded it with bg, and now you want it back where it belongs: in front of you, with your keyboard attached.

Why Learn fg?

  • Recover suspended work: Ctrl-Z freezes a job; fg %1 unfreezes it and hands your terminal back.
  • Bring background work into view: a job you started with & can be pulled forward with fg when you want to watch or feed it input.
  • Complete the job-control trio: jobs tells you what exists, bg keeps things running, fg reattaches.
  • One command, obvious behavior: fg with no arguments even resumes the most recent job.

fg is the “put it back” half of job control — the command you reach for after Ctrl-Z, and the one that makes backgrounding reversible.


1. Basic Usage: Bring a Job Back

# Start something in the background
sleep 300 &
[1] 12345

# Bring it to the foreground
fg %1
  • The job resumes in the foreground; your terminal is attached to it again.
  • Ctrl-C or Ctrl-Z now act on that job — job control is back to normal.

Check the job numbers first if you are unsure:

jobs
[1]+  Running                 sleep 300 &

2. Job Shortcuts

fg %+      # the current job (the one marked +)
fg %1      # job number 1
fg %sleep  # a job whose command starts with "sleep"
fg %?vim   # a job whose command contains "vim"
fg -       # the previous job (bash)
  • fg with no arguments resumes the current job (%+) — the same job bg would have used.
  • %name and %?str match by command prefix or substring, so you can skip memorizing numbers.

3. Stop a Running Job, Then Foreground It

# A running background job
sleep 300 &

# Suspend it (if it is running, Ctrl-Z only works in the foreground —
# so stop it from another terminal or use kill):
kill -STOP %1

# Now bring it to the foreground
fg %1
  • fg works on both stopped jobs and (in bash) running background jobs — the latter get suspended-then-foregrounded as needed.
  • The common case remains: Ctrl-Z → fg restores exactly where the job left off.

4. fg vs. bg: The Same Job, Two Directions

You wantUse
Job running but out of the waybg
Job back in front of youfg
# Started in foreground, want it backgrounded:
sleep 300
# Ctrl-Z
bg %1        # keep it running, terminal free

# Later, want it back:
fg %1        # terminal reattached
  • bg and fg are mirror images: same job numbers, opposite directions.

5. What fg Does NOT Do

  • It does not detach anything — for jobs that must survive logout, use nohup or a systemd unit.
  • It does not work across shells — fg only sees this shell’s jobs (see jobs).
  • It cannot resurrect a job that already finished; finished jobs are not resumable.

Common Pitfalls

  • Forgetting the job number: fg with no argument grabs the current job — if that is not the one you meant, be explicit: fg %2.
  • fg on a finished job fails: check jobs first; a Done job cannot be brought back.
  • Output floods your terminal: foregrounding a chatty job means its output lands on your screen again — that is the point, but it can be messy.
  • Suspending with Ctrl-C instead of Ctrl-Z: Ctrl-C kills; Ctrl-Z suspends. fg restores suspended jobs, not killed ones.
  • Job numbers reset per shell: they are per-session handles, not system-wide — the same job has different numbers in different terminals.

PageRelationship
jobsLists the jobs fg acts on — find the number first.
bgThe opposite move: push the job back to the background.
killCan signal jobs too: kill %1.
nohupFor jobs that must outlive the shell.
psSystem-wide view; fg is shell-local.

fg Cheatsheet

CommandWhat It Does
fgForeground the current job.
fg %1Foreground job number 1.
fg %+Foreground the current job (explicit).
fg %nameForeground a job by command-name prefix.
fg %?strForeground a job whose command contains str.
jobs -sShow stopped jobs (candidates for fg).
Ctrl-ZSuspend the foreground job, preparing it for bg/fg.