Why run a Linux shell command with '&'?
Solution 1:
In addition to Martin's, Ash's and Kevin's answers, sometimes you'll see an ampersand used mathematically for a bitwise AND*:
$ echo $(( 11 & 7 ))
3
In case you're not familiar with bit operators:
11: 1011
7: 0111
-------- AND
3: 0011
In each position there's a one bit in the first number AND the second number, set that bit to one in the answer.
*The feature in Kevin's answer is referred to as a logical AND.
To elaborate on Ash's answer, when used in redirection the ampersand can tell the shell to copy a file descriptor. In this command echo "hello" > outputfile 2>&1
the ampersand causes any output that may go to standard error (stderr, file descriptor 2) to go to the same place as standard output (stdout, file descriptor 1, the default for the left side of >
). The >& outputfile
operator is shorthand for > outputfile 2>&1
.
Also, new in Bash 4, there are two new terminators for clauses in the case
command ;&
and ;;&
which affect whether a case "falls through" and, if so, whether the next test is performed.
Solution 2:
In a Bash shell script, the ampersand “&” is used to fork processes:
find -name hello &
It will cause the find command to be forked and run in the background (you can always kill it by its PID).
Solution 3:
Why run a Linux shell command with '&'?
To get your prompt back immediately, and run the process in the background.
What are the function of them?
nohup allows the background process to continue running even after the user logs out (or exits the initiating shell).
>& redirects both standard output and standard error into the log file.
& runs the whole thing in the background, giving you your prompt back immediately.
Explanation:
Every Linux process opens three I/O channels, an input "stdin", a standard output "stdout" and a standard error output "stderr". They can be used for binary, but they are traditionally text. When most programs see stdin close, they exit (this can be changed by the programmer).
When the parent shell exits, stdin is closed on the children, and (often, usually) the children exit as well. In addition, the children receive a software signal, SIGHUP, indicating the user has "hung up" (formerly, the modem) and the default here is to exit as well. (Note, a programmer can change all of this when writing the program).
So, what nohup does is give the child process a separate I/O environment, tying up the ins and outs to something not tied to the parent shell, and shielding the child from the SIGHUP signal. Once the user disconnects, you will see the nohup background process owned by init (process 1), not the user's shell.
However nohup
cannot do the job completely if the process is run in the foreground, so &
is used to run the program in the background, where it can happily stay running with or without a user logged in.
Solution 4:
In addition to @Martin's answer: The other use of ampersand (>&
as above) is to capture both stdout
and stderr
. Normally, if you redirected output to a file with only '>', you would only get the output to stdout
, missing any errors.