How To Run A Command After The Previous One Has Finished On Linux

how to run a command after the previous one has finished on Linux

This article explains how to run a command after the previous command has finished running. Using this, you can run not just 2 commands, but any number of commands, after the previous one has finished. This will work with Sh, Bash, Zsh and other shells.

You can run a command after the previous one has finished, depending on the previous command's exit status (if it failed or not) or regardless of this. So there are 3 possible situations:

  • run a command if the previous command exited successfully,
  • run a command if the previous command failed (if it has an exit status other than 0), or
  • run a command regardless of the exit status of the previous command (it doesn't matter if it failed or not)

Let's take a look at each of these cases below.

You might also like: How To Repeat A Command Every X Seconds On Linux

To run multiple commands successively (wait for the previous one to finish) only if the previous command exited successfully, use the && operator between the commands. For example (you can have any number of commands):

command1 && command2

If you don't care about the exit status of the previous command, and you just want to run a command after the previous one has finished, use the ; separator. Example (you can have any number of commands):

command1; command2; command3

What if you want to run a command after the previous one has finished running, but only if this previous command has failed (if it returns an exit status other than 0). In that case, use the || separator. For example (once again, you can have any number of commands):

command1 || command2

You might also be interested in: How To Find Files Modified In The Last N Days Or Minutes Using find

You can also mix these operators. In that case, you can group the commands using { and }. For this to work, you need to make sure you add a semicolon (;) before } and a space after {. For example:

command1 || { command2; command3; }

In this example:

  • run command1 and wait for it to finish
  • if command1 fails, proceed to the next commands (||):
    • run command2 and wait for it to finish
    • regardless of command2's exit status, run command3 (;)

Another example:

{ command1 && command2; }; { command3 && command4; }

In this example:

  • run command1 and wait for it to finish
  • if command1 is successful, run command2 and wait for it to finish (&&)
  • regardless if command2 was successful or not, or if it even ran or not, run command3 and wait for it to finish (the ; after the first })
  • if command3 is successful, run command4 (&&)

As a reminder, the ; before each } is required when using {}, and does not affect the way the commands are run.

[[Edit]] The article initially used parentheses (()) for mixing the operators; but that has some drawbacks (like running the commands in a subshell) compared to using braces ({}), as pointed out by Laurent in a comment below. So I have updated the article to use {} for this.

You might like: How To Change The Default Shell In Linux