Open In App

bg command in Linux with Examples

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Linux, the bg command is a useful tool that allows you to manage and move processes between the foreground and background. It's especially helpful when you want to multitask in the terminal by placing a process in the background, enabling you to continue using the terminal for other commands while the process runs quietly in the background.

Syntax

bg [job_spec ...]

where,

  • job_spec: This is used to identify the job you want to move to the background. It can be specified in several formats:
    • %n: Refers to job number n.
    • %str: Refers to a job that was started by a command beginning with str.
    • %?str: Refers to a job that was started by a command containing str.
    • %% or %+: Refers to the current job. Both fg and bg commands will operate on this job if no job_spec is provided.
    • %-: Refers to the previous job.

If no job_spec is provided, the most recent job is resumed in the background.

Useful Options for bg command

1. bg [JOB_SPEC]:

This command is used to put the mentioned job in background. In the below screenshot, we do following : bg [JOB_SPEC]'sleep 500' is used to create dummy foreground job.

  • We use jobs command to list all jobs
  • We create a process using sleep command, we get its ID as 1.
  • We put it in background by providing its ID to bg.

2. bg --help:

Displays the help information for the bg command. This is useful if you need more information on how to use the command or if you're unsure of the available options. bg --help

Conclusion

Mastering the bg command in Linux can greatly improve your productivity by allowing you to manage processes more efficiently. Whether you're running long scripts, downloads, or background tasks, knowing how to suspend and resume jobs with bg will give you more control over your terminal environment.


Next Article

Similar Reads