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-Zfreezes a job;fg %1unfreezes it and hands your terminal back. - Bring background work into view: a job you started with
&can be pulled forward withfgwhen you want to watch or feed it input. - Complete the job-control trio:
jobstells you what exists,bgkeeps things running,fgreattaches. - One command, obvious behavior:
fgwith no arguments even resumes the most recent job.
fgis the “put it back” half of job control — the command you reach for afterCtrl-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-CorCtrl-Znow 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)
fgwith no arguments resumes the current job (%+) — the same jobbgwould have used.%nameand%?strmatch 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
fgworks on both stopped jobs and (in bash) running background jobs — the latter get suspended-then-foregrounded as needed.- The common case remains:
Ctrl-Z→fgrestores exactly where the job left off.
4. fg vs. bg: The Same Job, Two Directions
| You want | Use |
|---|---|
| Job running but out of the way | bg |
| Job back in front of you | fg |
# 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
bgandfgare 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
nohupor a systemd unit. - It does not work across shells —
fgonly sees this shell’s jobs (seejobs). - It cannot resurrect a job that already finished; finished jobs are not resumable.
Common Pitfalls
- Forgetting the job number:
fgwith no argument grabs the current job — if that is not the one you meant, be explicit:fg %2. fgon a finished job fails: checkjobsfirst; 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-Ckills;Ctrl-Zsuspends.fgrestores 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.
Related Pages
| Page | Relationship |
|---|---|
jobs | Lists the jobs fg acts on — find the number first. |
bg | The opposite move: push the job back to the background. |
kill | Can signal jobs too: kill %1. |
nohup | For jobs that must outlive the shell. |
ps | System-wide view; fg is shell-local. |
fg Cheatsheet
| Command | What It Does |
|---|---|
fg | Foreground the current job. |
fg %1 | Foreground job number 1. |
fg %+ | Foreground the current job (explicit). |
fg %name | Foreground a job by command-name prefix. |
fg %?str | Foreground a job whose command contains str. |
jobs -s | Show stopped jobs (candidates for fg). |
Ctrl-Z | Suspend the foreground job, preparing it for bg/fg. |