fg
Bring a background or stopped job to the foreground
By CMD Script Team · 4 min read · Last updated
fg [%JOB_SPEC]Options
| Flag | Description |
|---|---|
%n | Bring job number n to the foreground (e.g. %2) |
%+ or %% | Bring the current (most recently referenced) job to the foreground |
%- | Bring the previous job to the foreground |
%string | Bring the job whose command line starts with string to the foreground |
%?string | Bring the job whose command line contains string to the foreground |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
fg is a shell builtin that resumes a stopped or backgrounded job and moves it into the
foreground, attaching it to the terminal so it can receive input and its output appears
directly in the session. The shell prompt does not return until the job exits, is
suspended again (Ctrl-Z), or is sent back to the background.
Beginner examples
fg— bring the most recent (current) job back to the foregroundfg %1— bring job number 1 to the foregroundjobs— list background/stopped jobs and their numbers before usingfg- Press
Ctrl-Zto suspend a running foreground command, thenfgto resume it
fg %1
Advanced examples
- Bring back a job by matching its command text:
fg %vimresumes the suspended job whose command line starts withvim. - Chain job control in a single line: start a long job with
&, do other work, thenfg %1to reattach and watch it finish. - Use
%%or%+explicitly when scripting interactively to be unambiguous about which job is "current" versus%-(the "previous" job). - Combine with
disownsemantics: if youfga job and it's misbehaving,Ctrl-Zit again andkill %1instead of letting it run.
sleep 300 &
jobs
fg %1
Common mistakes
- Trying to run
fgfrom a different terminal/shell session than the one that started the job — jobs are tracked per-shell, not system-wide, sofgwon't see it. - Forgetting the
%prefix and runningfg 2instead offg %2(some shells tolerate this, but it's not portable). - Assuming
fgrestarts a program from scratch — it resumes the existing suspended process exactly where it was paused, it does not re-run the command. - Not realizing a job must exist in the current shell's job table (visible via
jobs) beforefgcan reference it; closing the terminal ends the jobs with it unless disowned or run undernohup/a multiplexer.
Tips
- Run
jobsfirst if you're not sure which job number to target — the+marks the default jobfgpicks with no argument. fgcombined withCtrl-Zandbgforms the core of interactive job control: suspend withCtrl-Z, then choosebgto keep it running in the background orfgto keep working with it directly.- In scripts, avoid relying on
fg/bg/job control — it's an interactive shell feature and behaves inconsistently in non-interactive shells.
Best practices
- Use
tmuxorscreeninstead of relying purely onfg/bgjob control for long-running tasks you want to detach from and reattach to across sessions. - Don't leave critical long-running work as a plain backgrounded shell job in an
interactive terminal — closing the terminal can terminate it unless it was disowned
or started with
nohup. - Check
jobsbefore terminating a shell session to make sure you haven't left important suspended or background work unresumed.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Accidentally suspending a long
vimorsshsession withCtrl-Zand usingfgto get back into it instead of losing your place. - Pausing a database import mid-way to check something else in the same terminal, then
fgto bring it back and watch it complete. - Managing multiple interactive tools in one terminal session by toggling between them
with
Ctrl-Z,bg,jobs, andfg.
Common interview questions
- What's the difference between
fgandbg?fgresumes a job and attaches it to the terminal in the foreground, blocking the prompt;bgresumes it but keeps it running in the background, returning the prompt immediately. - How do you list and select a specific job with
fg? Runjobsto see job numbers, thenfg %nto bring jobnto the foreground. - What happens if you close the terminal with a job still running via
fg/bgcontrol? By default the job receivesSIGHUPand is terminated unless it was started withnohupor disowned, since job control is tied to the shell session.
Frequently Asked Questions
What's the difference between fg and bg?
fg resumes a job and attaches it to the terminal in the foreground, so it can receive keyboard input and blocks the shell prompt until it finishes or is suspended again. bg resumes a job but leaves it running in the background, so the shell prompt returns immediately.
How do I bring a specific job back with fg?
Run jobs to list job numbers, then use fg %n where n is the job number, e.g. fg %2. Without an argument, fg operates on the current job (marked with + in jobs output).
What do I do if I pressed Ctrl-Z by accident?
Ctrl-Z suspends the current foreground process and returns you to the shell prompt. Run fg (or fg %1) immediately afterward to resume it in the foreground exactly where it left off.
Why does fg say 'no such job'?
The job number or name you specified doesn't match any entry in the shell's job table — either it already finished, it was run in a different shell session, or you mistyped the job spec. Run jobs first to confirm the correct number.
Cheat sheet
Download a quick-reference cheat sheet for fg.