I have a startup script line:
pyprogramm >> /dev/null 2>&1 &
Meanings:
>> /dev/null - redirect stdout to null device
2>&1 - redirect stderr to stdout (that is redirected to null device)
but what does the very last &
mean?
I have a startup script line:
pyprogramm >> /dev/null 2>&1 &
Meanings:
>> /dev/null - redirect stdout to null device
2>&1 - redirect stderr to stdout (that is redirected to null device)
but what does the very last &
mean?
What is the & terminator ?
The trailing
&
operator at the end of a command is used to put commands into background. This is actually a standard syntax specified by POSIX standard:The purpose of backgrounding commands is to run a command without the main shell in script or interactive shell waiting for command, which would block execution of other commands and make an inconvenience for the user to wait. This is handy for starting long-running commands but you need to continue work in the current shell. As you can guess, this originated from the time where there were no multi-tab terminal emulators, but terminals were actual physical hardware connected to a computer itself.
From the definition you can see that
&
also serves as command terminator for lists of commands, much like;
does. In your specific example,pyprogramm >> /dev/null 2>&1 &
there is only one command in the list.Sequential ; lists vs asynchronous & lists
More generally,
and
are two examples of lists terminated by the
;
and&
operators. One difference is that&
terminated list will have input connected to/dev/null
if job control is disabled:In sequential list, however, each command still has
stdin
connected to terminal if there are no explicit redirections.Note also, that from the definition we mentioned earlier,
&
executes commands in subshell. By contrast, the;
terminated list is executed in current shell. There's also difference in exit statuses. For&
the standard says:This is significant when you want to put multiple commands in background. When you write a script or command you will have to chose commands for which you don't care if they failed or not, or you will have to find a way to handle the non-zero ( error ) exit status. In your specific example,
pyprogramm >> /dev/null 2>&1 &
running in background should have some way of indicating if it failed or not, however judging that you use2>&1
you are hiding error output by redirecting, and you probably assume the script should not fail.By contrast,
;
exit status is defined as:Again, this has implications on how you write a sequential list of commands in command-line and how you want things to be handled if some of the commands in the list failed.
Side notes and additional reading
The fact that this is POSIX definition means that all Bourne-like shells, meaning
bash
,dash
, andksh
must support it.&
in redirection is different from&
as command terminator. It means duplicating (copying) the file-descriptor object. See What does & mean exactly in output redirection?In
bash
there is also|&
operator ( note no space between pipe and ampersand ). From bash manual:It means run the command in the background. The calling script continues rather than blocking until the called command completes.
&
this returns control of the script to the operating system but sent the response to the background not asnohup
that directly sends the entire execution