How do I suspend and resume a sequence of commands

2020-04-07 19:37发布

I have cmd2 that needs to follow after cmd1 completes. I need to pause cmd1 sometimes.

I type in

$ cmd1 && cmd2

and then press Ctrl+Z (Stop) to stop cmd1. Now, cmd1 is paused but when I resume, it does not start cmd2 after completion of cmd1.

I type in

$ cmd1 ; cmd2

and then I press Ctrl+Z (Stop) to stop cmd1. Now cmd1 is paused but it immediately starts cmd2. However, I wish to start cmd2 only after cmd1 finishes.

I did some research and someone suggested an elegant way in zsh but I wonder if there is an elegant way of doing it in bash.

标签: linux bash unix
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-04-07 19:52

Run it in a subshell:

(cmd1 && cmd2)

Example:

$ (sleep 5 && echo 1)                        # started the command chain
^Z
[1]+  Stopped         ( sleep 5 && echo 1 )  # stopped before `sleep 5` finished
$ fg                                         # resumed
( sleep 5 && echo 1 )
1                                            # `sleep 5` finished and `echo 1` ran
查看更多
登录 后发表回答