man bash (See JOB CONTROL)
& is a control operator, meaning it performs a control function.
If a command is terminated by the control operator &, the shell
executes the command in the background in a sub‐shell.
(A subshell is assigned a PID-Process ID Number)
In short, anything that is a command can be told to run in the background by terminating the command with &, which spawns a subshell process to run the command in.
Loosely, if it can be run stand-alone in a shell then it can be run in the background in a subshell (what
& does). If it's a complete instruction for the subshell that its run in or sequence of complete instructions (function) then it can be run in the background...maybe I'll be corrected here but it's tempting to say that if it can be run in the foreground in a shell then it can be run in the background in a subshell, with varying and customizable scope for its variables (contrast
&, and see
lastpipe, but scope of variables is a result of commands running in subshells, not because the commands are running in the background).
What constitutes a command?
From Wooledge Bash Guide:
BASH reads commands from its input (which is usually either a terminal
or a file). Each line of input that it reads is treated as a command —
an instruction to be carried out. (There are a few advanced cases,
such as commands that span multiple lines, that will be gotten to
later.) Bash divides each line into words that are demarcated by a
whitespace character (spaces and tabs). The first word of the line is
the name of the command to be executed. All the remaining words become
arguments to that command (options, filenames, etc.).
From aosa-bash-full.pdf in bash-doc-3.2 from Bash Source:
A “simple” shell command...consists of a command name, such as echo or cd, and a list of zero or more arguments and redirections. (pg 47)
Shell functions and shell scripts are both ways to name a group of commands
and execute the group, just like executing any other command. Shell
functions are declared using a special syntax and stored and executed
in the same shell’s context; shell scripts are created by putting commands into a file and executing a new instance of the shell to
interpret them. Shell functions share most of the execution context
with the shell that calls them, but shell scripts, since they are
interpreted by a new shell invocation, share only what is passed
between processes in the environment.
Bash introduces the concept of a job which is essentially a command
being executed by one or more processes. A pipeline, for instance,
uses one process for each element of the pipeline. The process group
is a way to join separate processes together into a single job. The
terminal has a process group ID associated with it, so the foreground process group is the one whose process group ID is the same as
the terminal’s. (pg 69)
- Any process can be run in the background
- a shell itself is a process
- any instruction/command (still loosely defined) within a shell can be run in the background by running it in the subshell that & spawns.
The biggest consideration is how the background subshell process interacts with its parent process, again, see Job Control in man bash