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?

0

1 Answer 1

35

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 
2
  • 10
    You can also use cmd1 & cmd2. This way cmd2 won't wait for cmd1 to finish before running. Commented Sep 29, 2015 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.) Commented Feb 29, 2024 at 14:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.