Master the Linux ‘bg’ Command: A Comprehensive Guide for Newbies

Peter Hou
2 min readJun 7, 2023

--

This article provides a comprehensive guide on the ‘bg’ command in Linux. From its historical background to its usage, parameters, common use cases, and advanced techniques, the article covers a range of topics aimed at equipping Linux beginners with the knowledge they need to utilize this command effectively.

Instructions

This article offers a detailed guide for Linux beginners who want to grasp the ‘bg’ command. We’ll explore its history, usage, parameters, and some advanced tricks.

History

The ‘bg’ command originates from Unix and Unix-like operating systems, and it stands for “background.” It’s part of the job control commands, which also include ‘fg’, ‘jobs’, etc.

When and why to use it

The ‘bg’ command is primarily used when you wish to run a job/process in the background after it has been stopped or paused. This is particularly handy when you’ve started a long-running process and then realize you need to do something else in the same terminal.

How to use it

To use the ‘bg’ command, simply type ‘bg’ followed by the job number of the process you want to move to the background.

$ bg %1

The commonly used parameters

The ‘bg’ command does not have parameters as such. It works primarily with job IDs. However, you can use it with certain job identifiers:

  • %n: refers to the job with the number n.
$ bg %1
  • %% or %+: refers to the current job.
$ bg %%

Other supported parameters

There are no other supported parameters for ‘bg’ as such. The ‘bg’ command primarily deals with job control, and it takes job identifiers instead of traditional parameters or options.

Most common use cases

A common scenario to use the ‘bg’ command is when you’ve started a process and realized you want it to run in the background, so you can continue to use the terminal.

$ bg %1

The tricky skills

The ‘bg’ command can be combined with other job control commands like ‘fg’, ‘jobs’, ‘kill’, etc., to manage your processes more effectively. One such trick is to pause a running job using ‘ctrl+z’, move it to the background with ‘bg’, and then resume it later with ‘fg’.

$ bg %1
$ jobs
$ fg %1

What needs to be noted

  • The ‘bg’ command is shell built-in, and its behavior might slightly vary from shell to shell.
  • A job moved to the background with ‘bg’ will stop if it requires user input.
  • Be mindful that if you close the terminal, all background jobs will be terminated.

Conclusion

Understanding the ‘bg’ 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 terminal usage more efficient.

--

--

Peter Hou

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