Question

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) &?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top