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 nmap Command: Scan Authorized Hosts and Ports

Linux nmap Command: Scan Authorized Hosts and Ports

Learn how to scan Authorized Hosts and Ports with the Linux nmap command, including practical examples, key options, and important precautions.

Linux parted Command: Inspect and Manage Partitions

Linux parted Command: Inspect and Manage Partitions

Learn how to inspect and Manage Partitions with the Linux parted command, including practical examples, key options, and important precautions.

Linux nc Command: Test TCP and UDP Connections

Linux nc Command: Test TCP and UDP Connections

Learn how to test TCP and UDP Connections with the Linux nc command, including practical examples, key options, and important precautions.

Linux grep Command: Search Text and Filter Command Output

Linux grep Command: Search Text and Filter Command Output

Learn how to use the Linux grep command to search files for text or regular expressions and filter command output, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux pkill Command: Signal Processes by Name

Linux pkill Command: Signal Processes by Name

Learn how to signal Processes by Name with the Linux pkill command, including practical examples, key options, and important precautions.

Linux Tutorial / Understand Users, Groups, and File Ownership

Linux Tutorial / Understand Users, Groups, and File Ownership

Understand UIDs, GIDs, primary and supplementary groups, and file ownership, then inspect them safely with id, groups, getent, ls, and stat.

Linux apropos Command: Search Commands by Description

Linux apropos Command: Search Commands by Description

Learn how to use the Linux apropos command to find relevant manual pages by searching description keywords, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux pwd Command: Print the Current Working Directory

Linux pwd Command: Print the Current Working Directory

Learn how to use the Linux pwd command to print the logical or physical absolute path of the current working directory, with essential options, practical examples, output interpretation, and common troubleshooting tips.

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Linux lsblk Command: Inspect Disks, Partitions, and Mounts

Learn how to inspect Disks, Partitions, and Mounts with the Linux lsblk command, including practical examples, key options, and important precautions.

Linux mktemp Command: Create Secure Temporary Files and Directories

Linux mktemp Command: Create Secure Temporary Files and Directories

Learn how to create unpredictable temporary files and directories with Linux mktemp, use templates and TMPDIR, clean up with trap, and avoid unsafe dry runs.