- Unix Commands Reference
- Unix Commands - Home
fg Command in Linux
The fg command in Linux brings the background process to the foreground. The fg stands for foreground and is a jobs control command. It originates from Unix and Unix-like operating systems. It is primarily used to bring a paused or running background job to the foreground for further operations.
Table of Contents
Here is a comprehensive guide to the options available with the fg command −
Syntax of fg Command
The syntax of the Linux fg command is as follows −
fg [jobID]
In the above syntax, the [jobID] field is used to mention the job ID of a job that is needed to bring to the foreground.
fg Command Options
The options used with the fg command are listed in the following table −
Options | Description |
---|---|
--help | It is used to display help related to the command |
%Number | It is used to specify the background job with a number |
%String | It is used to specify the background job whose name starts with the mentioned string |
%?String | It is used to specify the background job whose name contains the mentioned string |
%+ or %% | It is used to specify the most recent job |
%- | It is used to specify the previous job (the last job before the most recent one) |
Understanding Jobs in Linux
In Linux, any long-running process can be sent to the background to free up the terminal for other operations. For example, long-running commands such as downloading a large file or updating the operating system can occupy the terminal. In this situation, the job can be moved to the background using the bg command.
In Linux, a job can be sent to the background by using the ampersand sign (&). For example, to send a sleep job to the background, use −
sleep 30 &
Similarly, to suspend a job and send it to the background, the ctrl+Z shortcut key is used.
To list the background jobs, the jobs command is used and to list jobs with PIDs use jobs -l.
As can be seen in the output image, the suspended command is indicated as Stopped, while the running job is indicated as Running.
The fg job control command brings suspended and running background jobs to the foreground.
Examples of fg Command in Linux
This section demonstrates the usage of the Linux fg command with examples −
- Bringing the Most Recent Background Job to the Foreground
- Bringing the Previous Background Job to the Foreground
- Bringing a Specific Background Job to the Foreground
- Bringing a Background Job to the Foreground using a String
- Displaying Help
Bringing the Most Recent Background Job to the Foreground
To get the most recent background job to the foreground, use the fg command without any options.
fg
The %+ and %% options also bring the most recent jobs to the foreground.
fg %+ fg %%
Bringing the Previous Background Job to the Foreground
To bring the previous background job to the foreground, use the %- option with the fg command. The previous means the last job before the most recent one.
fg %-
Bringing a Specific Background Job to the Foreground
To bring a specific background job to the foreground, use the %Number option. For example, to bring the second job to the foreground, use −
fg %2
In the same way, any job can be brought to the foreground by mentioning the job number.
Bringing a Background Job to the Foreground using a String
To bring a background job whose name starts with a specific string use the %String option. For example, to bring a sleep job to the foreground, use −
fg %sleep
Similarly, to bring a background job whose name contains a specific string, the %?String option is used.
fg %?www
As can be seen in the output image, the command containing www has been brought to the foreground.
Displaying Help
To display help related to the fg command use the --help option.
fg --help
If you are using the fg command in a bash script, mention the set -m command in the script; otherwise, you may encounter no job control error. The set -m enables the jobs control in shell scripting. Moreover, if you try to bring a terminated job to the foreground, you will get no such job error. In such a case, use the jobs command to list the current background jobs.
Conclusion
The Linux fg command is used to bring background jobs to the foreground. It is known as a job control command, primarily used with the jobs command. It is a powerful tool to put forward any suspended or running background process to the foreground to perform further action.
In this tutorial, we covered the fg command, its syntax, options, and usage through various commands.