The fg (ForeGround) command moves the processes running in the background to the foreground, and the job id of the process is used to run the process in the foreground, for which job control must be enabled. Job control is simply the service used to manage the running processes, including the start, stop, and resume of the stopped processes.
This write-up will explain the usage of the fg command with the help of the examples. The post’s content is:
- How Does the fg Command Work in Linux?
- Example of the fg Command in Linux
- Bring the Background Job to Foreground
- Bring the Background Job to Foreground Using the String
- Bring the Background job to Foreground Using the Job ID
- Bring the Previous job to Foreground
Let’s get into the basics of the fg command.
How Does the fg Command Work in Linux?
To use the fg command in the Linux distributions, users must follow the mentioned-below general syntax:
$ fg job_spec
The explanation of the above command is:
- First, use the “fg” to revoke the command
- Then specify the job_spec, which is supposed to be a shift from the background to the foreground
Different symbols can also be used with the fg command whose explanation is given in the table below:
|%||This is used to display the previous job|
|%%||These are used to display the current job|
|%number||This is used to display the job number|
|%String||This is used to manage the job with the command|
|%?||This refers to the job that started with the command|
Now, we will understand the usage of the fg command with the help of some examples.
Example of the fg Command in Linux
To explain the usage of the fg command, we have first to run some jobs on the computer. To do so, run the below-mentioned command:
Stop the xeyes application with the shortcut key CTRL+Z. Also, ping the URL of itslinuxfoss with the command:
$ ping www.itslinuxfoss.com
And terminate the ping job with the shortcut key of “CTRL+Z”. Then, to confirm the execution of the commands, list down all the running jobs:
$ jobs -l
We can see the two jobs being stopped in the background whose job IDs are displayed.
Example 1: Bring the Background Job to Foreground
We can use the fg command to bring the background jobs to the foreground; for example, in the above example, we will resume the ping job using its job number, which is “1” and to do so, we have to use the command:
$ fg %1
The ping job has been resumed.
Example 2: Bring the Background Job to Foreground Using the String
The job has been resumed in the above example using the job number; this can also be done using the string. For example, we will use the command to bring it to the foreground from the background:
$ fg %ping
The ping command has resumed the job.
Example 3: Bring the Background job to Foreground Using the Job ID
The third method to bring the background job to the foreground is using its job id, for example, in our case, the job id of the ping job is 3829. We can also use this id to bring the process to the foreground:
$ fg %%3829
Example 4: Bring the Previous job to Foreground
Also, you can bring the previous job to the foreground. For example, to run the ping job again with the fg command, use the command:
$ fg %-
That’s all about using the fg command in Linux.
To use the fg command in Linux, follow the general syntax “fg [Job]”. You can use different symbols to bring the jobs to the foreground in Linux. In this blog, different examples have been used to explain the usage of the fg command in Linux using different symbols.