Skip to main content

How to Run a Command Only After the Previous Command is Unsuccessful in Bash

&& executes the command which follow only if the command which precedes it succeeds. || does the opposite:

update-system-configuration || echo "Update failed" | mail -s "Help Me" [email protected]

From bash man page:

# AND and OR lists are sequences of one of more pipelines separated by 
# the && and || control operators, respectively. AND and OR lists are
# executed with left associativity.

# COMMAND2 is executed if, and only if, COMMAND1 returns an exit status of zero

# COMMAND2 is executed if and only if COMMAND1 returns a non-zero exit status.
# The return status of AND and OR lists is the exit status of the last command
# executed in the list


  1. How do i run a command only after previous command is unsuccessful in bash