Master the Linux ‘fg’ Command: A Comprehensive Guide

Peter Hou
2 min readJun 7, 2023

--

This guide provides a comprehensive understanding of the Linux ‘fg’ command. Covering everything from its historical background to usage techniques, common parameters, and typical use cases, this article aims to equip Linux beginners with the necessary skills to effectively use this command.

Instructions

This guide aims to equip new Linux users with a thorough understanding of the ‘fg’ command. The article discusses its history, use-cases, usage techniques, and the most common parameters associated with it.

History

The ‘fg’ command, which stands for “foreground”, originates from Unix and Unix-like operating systems. It forms part of the job control commands, including others like ‘bg’ and ‘jobs’.

When and why to use it

‘fg’ is primarily used when you wish to continue a job that has been stopped or paused. It is particularly handy when you’ve started a long-running process and then realize that you need to work with it directly in the terminal.

How to use it

To use the ‘fg’ command, type ‘fg’ followed by the job number of the job you wish to bring to the foreground.

$ fg %1

The commonly used parameters

Similar to the ‘bg’ command, the ‘fg’ command itself doesn’t have parameters but works with job identifiers, for example:

  • %n is used to refer to job number n.
$ fg %1
  • %% or %+ is used to refer to the current job.
$ fg %%

Other supported parameters

The ‘fg’ command does not support other parameters. It primarily works with job control and accepts job identifiers rather than traditional parameters or options.

Most common use cases

One common use case for the ‘fg’ command is when you’ve started a process and realize that you want it to run in the foreground so you can interact with it directly.

$ fg %1

The tricky skills

The ‘fg’ command can be combined with other job control commands (like ‘bg’, ‘jobs’, ‘kill’ etc.) for more effective process management. A useful trick is to pause an ongoing job using ‘ctrl+z’, move it to the background with ‘bg’, and then bring it back to the foreground later using ‘fg’.

$ fg %1
$ jobs
$ fg %1

What needs to be noted

  • The ‘fg’ command is built into the shell, and its behavior may slightly vary across different shells.
  • Any background jobs that require user input will stop when moved to the background using ‘bg’ and will need to be resumed with ‘fg’.
  • Be aware that all background jobs will be terminated if you close the terminal.

Conclusion

Understanding the ‘fg’ command and job control in general is crucial when working with Linux. It allows you to effectively manage and control long-running processes, making your use of the terminal more efficient.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet