24

I have cmd1 and cmd2.

cmd1 && cmd2 will not run cmd2 if cmd1 fails.

cmd1 || cmd2 will run cmd2 if cmd1 fails

How do I run cmd2 regardless of whether cmd1 succeeds or not?

Cheetah
  • 603

1 Answers1

34

To execute cmd2 regardless of whether the previous one result is, separate your command with semicolons or newlines:

cmd1; cmd2

or

cmd1 cmd2

If set -e is enabled, add || true to ignore the result of the preceding command:

set -e

cmd1 || true; cmd2

or

cmd1 || true cmd2

ash
  • 103
hg8
  • 1,440
  • 10
    You can also use cmd1 & cmd2. This way cmd2 won't wait for cmd1 to finish before running. – Vinz Sep 29 '15 at 10:17
  • The suggestion does not work when the bash script has set -e enabled. In this case one can do cmd1 || true; cmd2. (Replace ; with new line if needed.) – ash Feb 29 '24 at 14:39