Linux fg Command: Bring a Shell Job to the Foreground

fg brings a background or stopped job of the current shell to the foreground. It is used when you want that job to receive terminal input again or to directly check its status.

What is the fg command?

When brought to the foreground, the shell prompt waits until the job ends or is stopped again. You can check the job number with jobs. The target of fg is not the PID but the job specifier of the current shell.

Basic syntax

fg [%job number]

Installed implementations and options may vary by distribution. Use help fg to check the description on your current system.

Examples

Check current shell jobs

Check which jobs are in the background.

jobs -l

Bring a specific job to the foreground

Replace the job number in the example with the actual value.

fg %1

Stop or terminate the job again

In foreground jobs, Ctrl+Z stops, and Ctrl+C usually sends an interrupt signal.

jobs

Bring background tasks to the foreground

fg brings a job from the current shell to the terminal foreground, allowing you to interact with it or wait until it completes. First, check the job number with jobs.

jobs
fg %1

For example, if you pause vim notes.txt with Ctrl+Z, bringing it back with fg %1 returns the editor screen to the terminal. The display and the return point depend on the program, so there is no fixed single-line output.

fg is not a command to take arbitrary PIDs from other terminals. If the job is already finished or is not a job of the current shell, it cannot be brought to the foreground. To pause a running program again, you generally use Ctrl+Z.

Main Options and Format

Options/Format Description
%N Select job number N of the current shell.
%+ Select the current job.
%- Select the previous job.
Omit argument Target the current job.

Precautions when using

Processes not managed by the current shell cannot be brought with fg. In non-interactive environments, job control may be turned off. To keep long jobs running even after the terminal is closed, consider using fg or a service management method instead of tmux.

Frequently Asked Questions

In fg %1, is %1 the PID?

No. It is the current shell's job number. Use the number shown in the jobs output.

Official Documentation

The exact behavior of the option and implementation-specific differences can be checked in the official fg documentation.

More in This Category
Linux unalias Command: Remove Command Aliases

Linux unalias Command: Remove Command Aliases

Learn how to use the Linux unalias command to remove one or all aliases from the current shell session, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux dnf Command: Manage RPM Distribution Packages

Linux dnf Command: Manage RPM Distribution Packages

Learn how to manage RPM Distribution Packages with the Linux dnf command, including practical examples, key options, and important precautions.

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Linux sudo Command: Run Commands with Elevated or Another User's Privileges

Learn how to use the Linux sudo command to run approved commands with elevated privileges or as another user, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux fg Command: Bring a Shell Job to the Foreground

Linux fg Command: Bring a Shell Job to the Foreground

Learn how to bring a Shell Job to the Foreground with the Linux fg command, including practical examples, key options, and important precautions.

Linux clear Command: Clear the Terminal Display

Linux clear Command: Clear the Terminal Display

Learn how to use the Linux clear command to clear the visible terminal screen and understand scrollback behavior, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux Tutorial / Read Files with cat, less, more, head, and tail

Linux Tutorial / Read Files with cat, less, more, head, and tail

Learn when to use cat, less, more, head, and tail to read Linux files, with a small reproducible example and guidance for long files and logs.

Linux apk Command: Manage Packages on Alpine Linux

Linux apk Command: Manage Packages on Alpine Linux

Learn how to manage Packages on Alpine Linux with the Linux apk command, including practical examples, key options, and important precautions.

Linux diff Command: Compare Text Files and Directories

Linux diff Command: Compare Text Files and Directories

Learn how Linux diff compares text files and directories, produces unified patches, ignores selected whitespace changes, and reports differences through exit status.

Linux journalctl Command: Read systemd Logs

Linux journalctl Command: Read systemd Logs

Learn how to read systemd Logs with the Linux journalctl command, including practical examples, key options, and important precautions.

Linux truncate Command: Shrink or Extend File Size

Linux truncate Command: Shrink or Extend File Size

Learn how to shrink or extend files with Linux truncate, adjust sizes relatively, match a reference file, and distinguish sparse logical size from disk usage.