98

I want to do something like this:

if cmd1 && cmd2
echo success
else
echo epic fail
fi

How should I do it?

3
  • 1
    You're just missing the "then" keyword before the "echo success" command. Commented Jun 22, 2011 at 10:29
  • 17
    Hmmm; your pseudocode seems to ask a different question than the one in your title.....
    – mattdm
    Commented Jun 22, 2011 at 14:43
  • Good resource for some shell scripting exit conditions pixelbeat.org/programming/shell_script_mistakes.html
    – Flashman
    Commented Jun 22, 2011 at 15:16

3 Answers 3

137

These should do what you need:

cmd1 && cmd2 && echo success || echo epic fail

or

if cmd1 && cmd2; then
    echo success
else
    echo epic fail
fi
4
  • 4
    This works, but I'm confused why || doesn't look at the output of the first echo command.
    – mlissner
    Commented Aug 19, 2015 at 21:32
  • 1
    @mlissner, the if else expects to exit codes, 0 if the command where launched and 1 if where errors. Do not read at the output. Just try whoami && whoami && echo success || echo epic fail and now whoami && whoareyou && echo success || echo epic fail. I cant figure out what you mean by "doesn't look at the output of the first echo command"
    – m3nda
    Commented Feb 9, 2016 at 14:41
  • 1
    @mlissner I think I got your question, but the answer is that echo command won't fail ever. That is, its return will be 0, i.e. truthy. So the condition that really matters is just cmd1 && cmd2 Commented Aug 23, 2017 at 11:25
  • printf "$(( 1/0 ))" || echo "failed" does not work. However, putting printf "$(( 1/0 ))" in step_1.sh, then running ./step_1.sh || echo "failed" behaves as expected.
    – Wassadamo
    Commented Aug 23, 2022 at 5:29
120

The pseudo-code in the question does not correspond to the title of the question.

If anybody needs to actually know how to run command 2 if command 1 fails, this is a simple explanation:

  • cmd1 || cmd2: This will run cmd1, and in case of failure it will run cmd2
  • cmd1 && cmd2: This will run cmd1, and in case of success it will run cmd2
  • cmd1 ; cmd2: This will run cmd1, and then it will run cmd2, independent of the failure or success of running cmd1.

For completeness, in your example you missed the then keyword. The code you tried to show would be written as

if cmd1 && cmd2; then echo success; else echo epic fail; fi

Which is equivalent to

((cmd1 && cmd2) && echo success) || echo epic fail

And parenthesis are optional due to left-associativity.

Note that in these codes, cmd2 will be run only in case of success of running cmd1, otherwise it echo fail directly.

33

Petr Uzel is spot on but you can also play with the magic $?.

$? holds the exit code from the last command executed, and if you use this you can write your scripts quite flexible.

This questions touches this topic a little bit, Best practice to use $? in bash? .

cmd1 
if [ "$?" -eq "0" ]
then
  echo "ok"
else
  echo "Fail"
fi

Then you also can react to different exit codes and do different things if you like.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .