tags:

views:

39

answers:

2

I'd like to have a command only execute if the preceding command's exit status was not 0.

i.e. Command 1 ^ Command 2 where Command 2 is only executed when Command 1 fails.

+3  A: 

For this, use the double-pipe (||) operator.

touch /asdf/fdasfds/fdasfdas || echo "Couldn't touch."

The second command is only executed when the first command returns non-zero, exactly as you specified.

Mark Rushakoff
I don't know why I didn't see this. Dumb oversight on my part. Thanks!
royvandewater
+1  A: 

This should work:

command1 || command2
Douglas Leeder