I want to background a command chain like cp a b && mv b c && rm a
.
I have tried doing cp a b && mv b c && rm a &
but this only backgrounds the last process.
How do I background a command chain?
cp a b && mv b c && rm a &
is correct. &
has lower precedence than &&
. In fact &
has lower precedence than anything other than ;
and newline: &
is in the same syntactic category as ;
, the difference being that ;
runs the command list in the foreground while &
runs it in the background. You can test this for yourself:
$ dash -c 'sleep 2 && echo waited & echo backgrounded'
backgrounded
$ waited
Same with pdksh, ksh93, bash, csh, tcsh.
The exception is zsh, which is weirdly incompatible. This is documented in the manual:
If a sublist is terminated by a
&
,&|
, or&!
, the shell executes the last pipeline in it in the background, and does not wait for it to finish (note the difference from other shells which execute the whole sublist in the background).
Unfortunately, zsh behaves in this way even in sh or ksh compatibility mode. To make sure that the whole command is executed in the background, put braces or parentheses around it. Parentheses create a subshell whereas braces don't, but this is irrelevant (except as a micro-optimization in some shells) since a backgrounded command is in a subshell anyway.
{ cp a b && mv b c && rm a; } &
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다