How to background a command chain?

Cobra_Fast

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?

Gilles 'SO- stop being evil'

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to chain command arguments to a specific command binary

분류에서Dev

how to use the background command?

분류에서Dev

How to run a command in background always?

분류에서Dev

How set command in background,close terminal and get it back to fg?

분류에서Dev

How can I play a song in the background via my command line?

분류에서Dev

How do I run a sudo command needing password input in the background?

분류에서Dev

How to run a program in background and also using && to execute another command

분류에서Dev

How to traverse the template chain in javascript

분류에서Dev

How to chain external commands in VIM?

분류에서Dev

How to chain scopes with ransack queries

분류에서Dev

PHP executing background command line script

분류에서Dev

How to stop a background service?

분류에서Dev

Sublime Text build system on Ubuntu: Cannot chain pkill command regardless of success

분류에서Dev

`jobs` command doesn't show any background processes

분류에서Dev

Set desktop background from command line over ssh

분류에서Dev

How to decouple input params for different handlers in a filter/chain?

분류에서Dev

How to set task ID of first task in a celery chain?

분류에서Dev

How do java implement hash map chain collision resolution

분류에서Dev

How do I 'grep' for the longest chain of lines matching a regexp

분류에서Dev

How to add a background image to string?

분류에서Dev

How to sprite body background in CSS

분류에서Dev

how to bring background color dynamically

분류에서Dev

How to set the background of a RelativeLayout the camera?

분류에서Dev

How to keep a Chronometer running in the background?

분류에서Dev

How to set background to views programmatically?

분류에서Dev

how to run closed application In background?

분류에서Dev

How to use video as a background in Android

분류에서Dev

How to redirect STDIN of background process?

분류에서Dev

How to change background at runtime on openscenegraph

Related 관련 기사

  1. 1

    How to chain command arguments to a specific command binary

  2. 2

    how to use the background command?

  3. 3

    How to run a command in background always?

  4. 4

    How set command in background,close terminal and get it back to fg?

  5. 5

    How can I play a song in the background via my command line?

  6. 6

    How do I run a sudo command needing password input in the background?

  7. 7

    How to run a program in background and also using && to execute another command

  8. 8

    How to traverse the template chain in javascript

  9. 9

    How to chain external commands in VIM?

  10. 10

    How to chain scopes with ransack queries

  11. 11

    PHP executing background command line script

  12. 12

    How to stop a background service?

  13. 13

    Sublime Text build system on Ubuntu: Cannot chain pkill command regardless of success

  14. 14

    `jobs` command doesn't show any background processes

  15. 15

    Set desktop background from command line over ssh

  16. 16

    How to decouple input params for different handlers in a filter/chain?

  17. 17

    How to set task ID of first task in a celery chain?

  18. 18

    How do java implement hash map chain collision resolution

  19. 19

    How do I 'grep' for the longest chain of lines matching a regexp

  20. 20

    How to add a background image to string?

  21. 21

    How to sprite body background in CSS

  22. 22

    how to bring background color dynamically

  23. 23

    How to set the background of a RelativeLayout the camera?

  24. 24

    How to keep a Chronometer running in the background?

  25. 25

    How to set background to views programmatically?

  26. 26

    how to run closed application In background?

  27. 27

    How to use video as a background in Android

  28. 28

    How to redirect STDIN of background process?

  29. 29

    How to change background at runtime on openscenegraph

뜨겁다태그

보관