Run one command after another, even if I suspend the first one (Ctrl-z)

前端 未结 2 1984
伪装坚强ぢ
伪装坚强ぢ 2020-11-30 19:46

I know in bash I can run one command after another by separating them by semicolons, like

$ command1; command2

Or if I only want comm

相关标签:
2条回答
  • 2020-11-30 19:59

    The following should do it:

    (command1; command2)
    

    Note the added parentheses.

    0 讨论(0)
  • 2020-11-30 20:01

    In Bash, when you place a job into the background (using CTRL+Z or &) it does not wait for the job to finish, and gives an exit code of zero (success). That much you have observed, and it is documented in the man pages.

    The behaviour of logical "AND", &&, is that it tests from left-to right. Each part must be successful, so if the first is unsuccessful then the second will not run. So with && it runs commands from left to right until one of them fails. The definition of success is an exitcode ($?) of zero.

    Contrast this with logical "OR", ||, which runs commands from left to right until one of them works.

    Explaination of the subshell solution give by @NPE can also be found in the man pages:

    Compound commands and command sequences of the form ‘a ; b ; c’ are not handled gracefully when process suspension is attempted. When a process is stopped, the shell immediately executes the next command in the sequence. It suffices to place the sequence of commands between parentheses to force it into a subshell, which may be stopped as a unit.

    The proper term for CTRL+Z is the suspend character, again from the man pages:

    Typing the suspend character (typically ^Z, Control-Z) while a process is running causes that process to be stopped and returns control to bash.

    (Sorry to quote the man pages so much, but they really are your friends and worth reading)

    If you look at stty -a you will see something like this:

    susp = ^Z; 
    

    So you can alter it, hence the phrase "typically". Don't do that though, it will confuse the heck out of everyone. The terminal driver raises a SIGTSTP signal which is trapped by Bash.

    0 讨论(0)
提交回复
热议问题