fg
Overview
fg resumes a job in the foreground, giving it the terminal again (stdin/stdout/signals). Use it to continue a stopped job interactively or to pull a background job back for interaction.
Syntax
fg [job_spec]Default is the current job (%+).
Job specification
| Spec | Meaning |
|---|---|
%n |
Job number n |
%name |
Command begins with name |
%?str |
Command contains str |
%% / %+ |
Current |
%- |
Previous |
Examples with Explanations
After Ctrl-Z
vim bigfile.txt
# Ctrl-Z
jobs
fg
# or
fg %vimClassic suspend-and-resume editor workflow.
Pull background job forward
./server &
jobs -l
fg %1
# Ctrl-C now kills the server (foreground signals)Switch between jobs
jobs
fg %1
# Ctrl-Z
fg %2With pipelines
tar czf - dir | ssh host 'cat > backup.tgz' &
jobs
fg %1Script note
set -m
sleep 5 &
fg %1Usually unnecessary — scripts should wait on PIDs instead.
Notes / Pitfalls
- Only jobs of the current shell.
- Foreground job receives terminal-generated signals (
Ctrl-C→ SIGINT,Ctrl-Z→ SIGTSTP). - If job is already foreground,
fgerrors. - Background jobs needing stdin will stop with SIGTTIN —
fgto provide input. - Job numbers change as jobs complete — verify with
jobsfirst.
2026-relevant notes
- Interactive recovery still common; long-running prod work belongs in systemd/tmux.
- SSH drop kills foreground jobs; use multiplexers for remote interactive work.
- Know difference:
fg(shell job) vsdocker attach/kubectl attach(container).
Additional Resources
help fg,man bash