Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I suspend and resume a sequence of commands in Bash?

Tags:

linux

bash

unix

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.

like image 923
Lance Ruo Zhang Avatar asked Jan 18 '17 05:01

Lance Ruo Zhang


1 Answers

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
like image 108
codeforester Avatar answered Oct 31 '22 18:10

codeforester