What is the fg command in Linux?

Overview

The fg command brings a background process in your current Linux shell to the foreground.

Syntax


fg [job_spec]

Below are the available ways to use job_spec to reference a job:

  • %- – Reference to the previous job.

  • %% or %+ – Reference to the current job.

  • %number – Represents the job number, e.g., %1 or %2.

  • %String – Reference to the job started by a command containing a string.

Code


> educative> sleep 30
> ^Z
> zsh: suspended sleep 30
> educative> jobs
> [1]  + suspended sleep 30
> educative> fg
> [1]  + continued sleep 30
> educative>

Explanation

Perform the steps in the terminal below:

  1. Start the sleep process with the sleep command.

  2. Press Ctrl + Z to suspend the process created in step 1. Now the process moves to the background.

  3. Use the jobs command to list all the jobs.

  4. Use the fg command to bring the sleep process to the foreground. This can be achieved in two ways:

    • Use the fg command.

    • Use the fg %1 command because the job ID is 1 for the sleep process in the image above.

Terminal 1
Terminal
Loading...

Free Resources