Linux / Unix: fg Command Examples

fg command

I am new Linux/Unix shell user. How do I bring a background process to the foreground on Linux or Unix-like system? How do I run jobs in the foreground on Linux/Unix bash/ksh or POSIX based shell?

Job control is nothing but the ability to stop/suspend the execution of processes (command) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell.[donotprint]

fg command details
DescriptionBring a background process
to the foreground
Root privilegesNo
Est. reading time10m
Table of contents
[/donotprint] You shell keeps a table of currently executing jobs and can be displayed with jobs command. You need to use bg command to restart a stopped background process. The fg command moves a background job in the current shell environment into the foreground.


Place job in the foreground, and make it the current job.


The basic syntax is as follows:

fg jobID


fg jobID1 jobID2 ... jobIDN

Understanding the job number (jobID)

There are a various ways to refer to a job in the shell. The character % introduces a job specification. The JobID can be a process ID (PID) number, or you can use one of the following symbol combinations:

  1. %Number : Use the job number such as %1 or %2.
  2. %String : Use the string whose name begins with suspended command such as %commandNameHere or %ping.
  3. %+ OR %% : Refers to the current job.
  4. %- : Refers to the previous job.

fg command examples

Before you start using fg command, you need to start couple of jobs on your system for demonstration purpose. Type the following commands to start jobs:

xeyes &
gnome-calculator &
gedit &

Finally, run ping command in foreground:


To suspend ping command job hit the Ctrl-Z key sequence.

Finding jobs that are running or suspended in background

Type the following jobs command:
$ jobs -l
The output of the jobs -l command shows the following job running in the background:

[4]+  6138 Stopped                 ping

How do I bring a background ping command job to the foreground?

To bring a background or suspended process called ping to the foreground, enter:
$ fg %4
$ fg %ping
The screen displays ping command as follows:

Fig.01 fg command in action

Fig.01 fg command in action

fg command options

From the bash(1) command man page or run help fg command to see all options:

fg: fg [job_spec]
    Move job to the foreground.
    Place the job identified by JOB_SPEC in the foreground, making it the
    current job.  If JOB_SPEC is not present, the shell's notion of the
    current job is used.
    Exit Status:
    Status of command placed in foreground, or failure if an error occurs.

A note about /usr/bin/fg and shell builtin

Type the following type command to find out whether fg is part of shell, external command or both:
$ type -a fg
Sample outputs:

fg is a shell builtin
fg is /usr/bin/fg

In almost all cases you need to use the fg command that is implemented as a BASH/KSH/POSIX shell built-in. The /usr/bin/fg command can not be used in the current shell session. The /usr/bin/bg command operates in a different environment and does not share the parent bash/ksh’s shells understanding of jobs.

Related media

This tutorial is also available in a quick video format:

See also
  • bash(1) Linux/Unix command man page
  • ksh(1) Linux/Unix command man page

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • aref ghobadi Aug 17, 2015 @ 9:35

    thanks a lot

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum