Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The & (ampersand) is a control operator in Bash that instructs the shell to execute a command asynchronously in the background. When a command is terminated by &, the shell forks a subshell to run the process, immediately evaluates to an exit status of 0 within the current shell (setting the special parameter $? to 0), and resumes execution without waiting for the background process to terminate.
command [arguments] &

Technical Mechanics

Process Creation and Job Control When the shell encounters the & operator, it utilizes the fork() system call to create a child process. The parent shell does not invoke wait() on this child process immediately. If job control is enabled (which is the default in interactive shells), Bash registers the process in its internal job table and prints the job number (enclosed in brackets) and the Process ID (PID) to standard error.
$ sleep 60 &
[1] 45892
In standard Bash scripts where job control is disabled by default, this standard error output is suppressed. Special Parameters The PID of the most recently executed background command is automatically stored in the special parameter $!. This allows the parent shell to track, signal, or synchronize with the asynchronous child process.
command &
PID=$!

Standard Streams Behavior

  • Standard Input (stdin): The behavior of standard input depends on the job control state. If job control is active, a background process attempting to read from the terminal will receive a SIGTTIN signal from the terminal driver, which suspends the process until it is brought to the foreground. If job control is inactive (as in scripts), Bash automatically redirects the background process’s standard input from /dev/null unless it is explicitly redirected elsewhere.
  • Standard Output (stdout) and Standard Error (stderr): Unless explicitly redirected, the background process inherits the parent shell’s file descriptors for stdout and stderr. Output generated by the background process will print directly to the controlling terminal, interleaving with the parent shell’s output.

Signal Handling

When job control is not active (the default behavior in scripts), asynchronous commands executed with & automatically ignore SIGINT (interrupt) and SIGQUIT (quit) signals. This prevents terminal-generated signals intended for the foreground process group from terminating detached background tasks.

Synchronization

Because the & operator detaches the execution flow, the parent shell must use the wait built-in command to synchronize with the background process. The wait command blocks the parent shell’s execution until the specified background job terminates, at which point it evaluates to the exit status of that specific background job.
command1 &
PID1=$!

command2 &
PID2=$!

wait $PID1 $PID2

Operator Precedence and Parsing

The & operator functions as a command terminator, structurally equivalent to a semicolon (;) or a newline. It has lower precedence than logical operators (&& and ||) and pipe operators (|). If used at the end of a pipeline, the & operator applies to the entire pipeline, placing all commands within that pipeline into the background as a single job.
command1 | command2 | command3 &
Master Bash with Deep Grasping Methodology!Learn More