Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ( ) & and ( &)?

Tags:

bash

subshell

I'm wondering what's the difference between these two grammar in bash: ( &) and ( ) &.

The only difference that I noticed is, (tty &) will return "not a tty" while (tty) & will return the current tty name, but why?

To give an example, should I run (setsid startx &) or (setsid startx) &?

like image 799
kawing-chiu Avatar asked Mar 16 '14 03:03

kawing-chiu


1 Answers

In the case of

(tty &)

a subshell is started which starts another tty process in the background without job control and terminal, hence there is a "not a tty" error. The tty process becomes detached with PPID 1

In the case of

(tty) &

a subshell is started and runs in the background. This background shell starts a tty process and after tty finishes and reports to the terminal, the subshell finishes in the background.

--

tty is a simple command. Whether or not a particular command (like startx) needs a ( ... &) construct to become detached / disowned from a parent process depends on the command itself. There are a number of ways for a process to in turn start a subprocess and detach that, so the command may not need it.

like image 62
Scrutinizer Avatar answered Sep 21 '22 16:09

Scrutinizer