>_cmd.script

fg

Bring a background or stopped job to the foreground

Processes

By CMD Script Team · 4 min read · Last updated

SYNTAX
fg [%JOB_SPEC]

Options

Command options and flags
FlagDescription
%nBring 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
%stringBring the job whose command line starts with string to the foreground
%?stringBring 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 foreground
  • fg %1 — bring job number 1 to the foreground
  • jobs — list background/stopped jobs and their numbers before using fg
  • Press Ctrl-Z to suspend a running foreground command, then fg to resume it
fg %1

Advanced examples

  • Bring back a job by matching its command text: fg %vim resumes the suspended job whose command line starts with vim.
  • Chain job control in a single line: start a long job with &, do other work, then fg %1 to 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 disown semantics: if you fg a job and it's misbehaving, Ctrl-Z it again and kill %1 instead of letting it run.
sleep 300 &
jobs
fg %1

Common mistakes

  • Trying to run fg from a different terminal/shell session than the one that started the job — jobs are tracked per-shell, not system-wide, so fg won't see it.
  • Forgetting the % prefix and running fg 2 instead of fg %2 (some shells tolerate this, but it's not portable).
  • Assuming fg restarts 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) before fg can reference it; closing the terminal ends the jobs with it unless disowned or run under nohup/a multiplexer.

Tips

  • Run jobs first if you're not sure which job number to target — the + marks the default job fg picks with no argument.
  • fg combined with Ctrl-Z and bg forms the core of interactive job control: suspend with Ctrl-Z, then choose bg to keep it running in the background or fg to 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 tmux or screen instead of relying purely on fg/bg job 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 jobs before 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 vim or ssh session with Ctrl-Z and using fg to get back into it instead of losing your place.
  • Pausing a database import mid-way to check something else in the same terminal, then fg to bring it back and watch it complete.
  • Managing multiple interactive tools in one terminal session by toggling between them with Ctrl-Z, bg, jobs, and fg.

Common interview questions

  • What's the difference between fg and bg? fg resumes a job and attaches it to the terminal in the foreground, blocking the prompt; bg resumes it but keeps it running in the background, returning the prompt immediately.
  • How do you list and select a specific job with fg? Run jobs to see job numbers, then fg %n to bring job n to the foreground.
  • What happens if you close the terminal with a job still running via fg/bg control? By default the job receives SIGHUP and is terminated unless it was started with nohup or 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.