Open In App

fg command in Linux with Examples

Last Updated : 06 Nov, 2025
Suggest changes
Share
6 Likes
Like
Report

The fg command in Linux is used to bring a background job to the foreground. It allows you to interact with the process directly through the terminal.

  • Resumes a background or stopped job in the foreground.
  • Useful for switching back to a process that was sent to the background with bg 0r &.

Example:

Command:

sleep 60 &
fg %3
fg

Syntax

fg [job_spec]

The job_spec is a way to refer to the background jobs that are currently running or suspended. Here are some common ways to specify a job:

  • %n: Refers to job number n.
  • %str: Refers to a job that was started by a command beginning with str.
  • %?str: Refers to a job that was started by a command containing str.
  • %% or %+: Refers to the current job (this is the default job operated on by fg if no job_spec is provided).
  • %-: Refers to the previous job.

Key Options for the fg command

1. fg [JOB_SPEC]:

This is the primary use of the fg command, bringing a specified job running in the background back to the foreground. For example, if you create a dummy job using sleep 500, you can bring it back to the foreground by referencing its job number: fg [JOB_SPEC]"sleep 500" is a command which is used to create a dummy job which runs for 500 seconds.

2. fg --help:

This option displays help information for the fg command, explaining usage and available options. fg --help

Suggested Quiz
4 Questions

What does the fg command do in Linux?

  • A

    Starts a new job in the background

  • B

    Stops a running job immediately

  • C

    Brings a background job to the foreground

  • D

    Deletes a running job

Explanation:

fg is used to move a background or stopped job back to the foreground.

Which fg syntax brings the job whose command starts with the text "sleep" to the foreground?

  • A

    fg %sleep

  • B

    fg %?sleep

  • C

    fg sleep

  • D

    fg %/sleep/

Explanation:

%str matches a job whose command begins with the specified text.

Which command brings job number 4 to the foreground?

  • A

    fg job4

  • B

    fg %4

  • C

    fg 4&

  • D

    fg @@4

Explanation:

%4 is the correct job specifier for job number 4.

A background job was started using the command "backup_data.sh". Which fg syntax correctly identifies the job using a pattern that matches any job containing the string "data"?

  • A

    fg %data


  • B

    fg %?data

  • C

    fg %%data

  • D

    fg backup_data.sh &

Explanation:

%?str matches a job whose command line contains the substring anywhere.

Quiz Completed Successfully
Your Score :   2/4
Accuracy :  0%
Login to View Explanation
1/4 1/4 < Previous Next >

Explore