Using the fg Command on Linux

In this quick guide, we will be exploring how to use the fg command on a Linux-based system.

fg command on Linux

The fg command is a part of the job control functionality of various shells. Some shells that support this functionality are csh, bash, tcsh, and ksh.

Using this command, you can bring a process from the background to the foreground of your shell, allowing you to interact with it directly. If the job is currently suspended, the shell will start it once it is brought to the foreground.

If you prefer to keep the job in the background but resume it running, you should use the bg command instead.

To get the ID of a job suspended or running in the background, you will need to utilize the jobs command.

Below we will go into the various ways to use the fg command within your terminal.

Syntax of the fg Command

The fg commands syntax only has one optional parameter that allows you to specify the job you want to bring to the foreground.

fg [JOBID]

Using the fg command without using a parameter will bring the last suspended command to the foreground.

However, using the JOBID field, you can pick the job you want to be brought to the foreground. The easiest way to get the exact job you want to restore is to get the ID from the jobs command.

  • %JOBID – If you know the ID for the job you want to restore, you can simply use the percentage sign (%), followed by its number.

    For example, if we had a job with the ID of 2, we would use “%2“.
  • %JOBSTRING – You can also use a string to restore a job to the foreground. This string can either be the whole command or the beginning of it.

    To reference a string, you must use a percentage sign (%), followed by the string.

    For example, if we had a ping command suspended in the background, we can bring it to the foreground by using “%ping“.
  • %+ or %% – The fg command allows you to select the last command you put into the background easily. This is typically referred to as the current job.

    You need to use the percentage sign (%) followed by a plus symbol (+). Alternatively, you can use two percentage sign symbols (%%).
  • %- – Finally, the fg command allows you to select the job used before the last one. This job is typically called the previous job.

    To reference the previous job, you need to use the percentage sign (%) followed by the minus symbol (-).

Examples of Using the fg Command

For this section, we will be walking you through some examples of using the fg command on your system.

Before we can use this command, we will need to have some jobs already started to bring them to the foreground.

You can suspend a currently running process by pressing CTRL + Z. Alternatively, you can use an ampersand symbol at the end of the command to push it to the background.

Using the fg Command with a Known Job ID

Every job is assigned its own unique ID when suspended or moved into the background. You can use this ID to move the job back to the foreground, thanks to the fg command.

Whenever you put a process into the background, you will be given its ID. Additionally, you can find these ids by using built-ins such as jobs.

For our example, we have the following jobs currently running within our shell session.

[1]+  Stopped                 ping google.com
[2]   Running                 chromium-browser &
[3]   Stopped                 nano exampletext.txt
[4]   Stopped                 ./examplescript.sh
[5]-  Stopped                 nano test

If we wanted to bring our “examplescript.sh” back to the foreground, we would need to use “%4” as the ID.

fg %4

Bringing a Process to the Foreground By String

You can also bring a process to the foreground by using the fg command followed by a string.

This string doesn’t need to match the command entirely. However, if two commands have similar strings, you must specify that job by its ID.

For this example, we had the following jobs running on our shell.

[1]    Stopped                 ping pimylifeup.com
[2]    Running                 chromium-browser &
[3]-   Stopped                 nano exampletext.txt
[4]+   Stopped                 ./examplescript.sh

For example, if we wanted to bring the ping job to the foreground, we can use the percentage sign (%), followed by “ping“.

fg %ping

Using fg on the Current Job

The last process that you move into the background will be marked as the “current” job. There are two ways that you can reference this using the fg command.

First, using the fg command without any parameters, will automatically select the current job.

fg

Alternatively, you can reference the current job using the “%+” symbols as the job id.

fg %+

Finally, it is also possible to use the “%%” symbols as the job ID to select the current job.

fg %%

Bringing the Previous Job to the Foreground

Finally, you can also select the previous job easily to bring to the foreground. The previous job is the one that was in the foreground before the “current” job.

To bring the previous job to the foreground using the fg command, we need to use the”%-” symbols.

fg %-

Conclusion

At this point in the guide, you should now know how to use the fg command within your Linux shell.

This is a useful utility that allows you to effortlessly bring a process from the background to the foreground of your shell. It is a key part of multitasking within the shell on a Linux system.

This command goes hand in hand with both the jobs command and the bg command. All together, they form a part of the shell jobs system.

If you have any questions about using the fg command, leave a comment below.

Please check out our many other Linux tutorials and command guides.

Leave a Reply

Your email address will not be published. Required fields are marked *