I have just realized that using || operator as a check to see if a command completed with successfully doesn't handle multiple commands after the ||.

Here is an exert from my script, I am waiting for a ping to come back and if it doesn't in a certain amount of time, then it should run two commands (in the real script, there are two functions, one to cleanup, and one to print the error and exit. I cant combine them).

echo "start"
timeout 10s bash -c 'until ping -w1 -c1 >/dev/null 2>&1; do :; done' || echo "unsuc"; exit 1
echo "suc"
exit 0

My problem is that if the ping succeeds, then the script just exit 1. I also tried doing echo "unsuc" && exit 1, but that results in the same behavior.

How do I run two commands if the ping command fails?

1 Answer 1


Use a group command (AKA "list") - e.g. ... || { echo "unsuc" ; exit 1 ; }.

See man bash and search for the Compound Commands section.

Alternatively, use an if statement, for example:

echo "start"
if ! timeout 10s bash -c 'until ping -w1 -c1 >/dev/null 2>&1; do :; done' ; then
  echo "unsuc"
  exit 1
echo "suc"
exit 0

You must log in to answer this question.