Infant Dev
Infant Dev

Reputation: 1749

In the shell, what is “ 2>&1 &”?

I understand that 2>&1 sends both stout and sterr to one file. But what does an & in the end means?

2>&1 &
#    ^ this one

Upvotes: 2

Views: 136

Answers (1)

It tells bash to run the job in the background, returning an interactive terminal to the user (see also here). You can get the job back to the foreground by entering

fg

If you want to run multiple commands in the background from a single command line statement, you need to encapsulate them in parentheses, e.g.

(cmd1 &); (cmd2 &);

If you would like to execute multiple commands together as one background process, you can group them using braces:

({ subcmd11; subcmd12; } &); ( { subcmd21; subcmd22; } &);

Please note that the braces need to be followed by a whitespace and that the list of commands inside braces must be terminated by a semicolon (regular parentheses do not have these limitations, but they create an additional subshell). For a good overview of the two grouping styles, check this reference.

Upvotes: 2

Related Questions