Overview
In Linux, you can run a job in the background using the `&` symbol, or suspend a foreground job by pressing `Ctrl+Z`. Jobs moved to the background release control of the terminal, interrupting user input or screen output. The `fg` command brings these jobs back to the foreground, regaining terminal control. It is commonly used with the job number obtained from the `jobs` command.
Key Features
Key features of the `fg` command include:
- Brings background jobs to the foreground.
- Allows specifying a particular job using its job number.
- Enables real-time viewing of the job's output again.
- Restores the state where the job can receive user input.
Difference between fg and bg
Both `fg` and `bg` control background jobs, but they have different roles.
- fg: Switches a background job to the foreground, taking control of the terminal.
- bg: Resumes a suspended job to continue running in the background.
Main Options
The `fg` command is primarily used with an argument (job number). There are no complex additional options.
1) Execution Options
Generated command:
Try combining the commands.
Description:
`fg` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Explore the various uses of the `fg` command through these examples.
Move a Job to Background, then to Foreground
sleep 100 &
jobs
fg %1
Execute `sleep 100` in the background (`&`), then check with `jobs` and bring it back to the foreground with `fg`.
Suspend a Job, then Bring to Foreground
ping google.com
(Ctrl+Z 입력)
jobs
fg
Run the `ping` command, suspend it with `Ctrl+Z`, then check with `jobs` and resume it with `fg`.
Bring the Second Job to Foreground
jobs
fg %2
Bring the second job (`%2`) from the list seen with `jobs` to the foreground.
Installation
`fg` is a built-in command of shells (like bash, zsh, etc.) and does not require separate installation.
Tips & Cautions
Here are some points to keep in mind when using the `fg` command.
Tips
- The `jobs` command allows you to see which jobs are currently in your shell; the number in brackets, like `[1]`, is the job number.
- If you do not specify an argument to the `fg` command, it will bring the most recently backgrounded or suspended job to the foreground.
- After bringing a background job to the foreground, if you want to send it back to the background, you can suspend it with `Ctrl+Z` and then use `bg`.