fg – Move Background Jobs to the Foreground Terminal

The fg command is a Linux shell command that allows users to bring background jobs to the foreground terminal. This command is used to resume a previously paused job that was running in the background.

Overview

To use the fg command, first, you need to have a job running in the background. You can run a job in the background by appending an ampersand (&) at the end of the command. For example, the following command runs the sleep command in the background for 10 seconds:

$ sleep 10 &
[1] 1234

The number inside the square brackets, [1], is the job ID, and 1234 is the process ID of the sleep command. Now, the sleep command is running in the background, and you can start another command in the foreground.

To bring the background job to the foreground, use the fg command followed by the job ID. For example:

$ fg %1

This command brings the job with ID 1 to the foreground. Once the job is in the foreground, you can interact with it as you would with any other foreground job.

If you have multiple background jobs, you can specify which job to bring to the foreground by using its job ID. For example:

$ fg %2

This command brings the job with ID 2 to the foreground.

Options

The fg command has no options.

Troubleshooting Tips

  • If the fg command fails to bring a background job to the foreground, it may be because the job has completed or has been terminated. You can check the status of all jobs by using the jobs command.
  • If you have multiple background jobs, you can use the jobs command to list all the jobs and their job IDs.

Notes

  • The fg command only works with jobs that were started in the current shell session. If you close the terminal or log out, any background jobs will be terminated.