What is the fg command in Linux?
Overview
The fg command brings a background process in your current Linux shell to the foreground.
Syntax
fg [job_spec]
Below are the available ways to use job_spec to reference a job:
-
%-– Reference to the previous job. -
%%or%+– Reference to the current job. -
%number– Represents the job number, e.g.,%1or%2. -
%String– Reference to the job started by a command containing a string.
Code
> educative> sleep 30
> ^Z
> zsh: suspended sleep 30
> educative> jobs
> [1] + suspended sleep 30
> educative> fg
> [1] + continued sleep 30
> educative>
Explanation
Perform the steps in the terminal below:
-
Start the
sleepprocess with thesleepcommand. -
Press
Ctrl + Zto suspend the process created in step 1. Now the process moves to the background. -
Use the
jobscommand to list all the jobs. -
Use the
fgcommand to bring thesleepprocess to the foreground. This can be achieved in two ways:-
Use the
fgcommand. -
Use the
fg %1command because the job ID is1for thesleepprocess in the image above.
-