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.
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.
I think it should be mentioned that that OR doesn't have the same meaning as XOR ("exclusive or"). See truth table below.
"or" "xor"
P Q (( $P || $Q )) (( ($P && ! $Q) || (! $P && $Q) ))
0 0 0 0
0 1 1 1
1 0 1 1
1 1 1 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With