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
>& outputfile operator is shorthand for
> outputfile 2>&1.
Also, new in Bash 4, there are two new terminators for clauses in the
;;& which affect whether a case “falls through” and, if so, whether the next test is performed.
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).
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.
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.
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.
In addition to @Martin’s answer: The other use of ampersand (
>& as above) is to capture both
stderr. Normally, if you redirected output to a file with only ‘>’, you would only get the output to
stdout, missing any errors.
In addition to Martin and Ash’s answer, sometimes you may see the use of an
&& token. This is used to say “run the second command if and only if the first command ran successfully.” A well-written command will, if it has any errors, not exit successfully.
[[email protected] ~]$ ls file && echo removing file && rm file ls: file: No such file or directory [[email protected] ~]$ touch file [[email protected] ~]$ ls file && echo removing file && rm file file removing file [[email protected] ~]$
Martin’s answer is good, but a bit ambiguous. The command is always fork’ed and exec’ed, but normal shell behavior is to wait on the command until it exits. The ampersand puts it into the background so that you get your terminal prompt back and you can do other things. If the process spits out data to stdout or stderr, this will be intermixed with whatever you’re doing at the prompt, and may confuse you. This is why you redirect then with >& /path/to/logfile.txt.
In response to George2, normal behavior for a shell exiting is to send the SIGHUP signal to all processes in the same process group (essentially stuff you spawned) and they usually will terminate. If you want this to continue even if you close the shell process, you can use the nohup command to make them ignore this signal and keep running. There’s a special name for this type of process, it’s called a daemon process (pronounced ‘demon’).