Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to do something like this:

if cmd1 && cmd2
echo success
echo epic fail

How should I do it?

share|improve this question
You're just missing the "then" keyword before the "echo success" command. –  glenn jackman Jun 22 '11 at 10:29
Hmmm; your pseudocode seems to ask a different question than the one in your title..... –  mattdm Jun 22 '11 at 14:43
Good resource for some shell scripting exit conditions –  Flashman Jun 22 '11 at 15:16

2 Answers 2

up vote 30 down vote accepted

These should do what you need:

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


if cmd1 && cmd2; then
    echo success
    echo epic fail
share|improve this answer
This works, but I'm confused why || doesn't look at the output of the first echo command. –  mlissner Aug 19 at 21:32

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? .

if [ "$?" -eq "0" ]
  echo "ok"
  echo "Fail"

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.