Bash/sh - difference between && and ;
I'm using &&
because a long time ago at the nearby computer:
root# pwd
/
root# cd /tnp/test; rm -rf *
cd: /tnp/test: No such file or directory
...
... and after a while ...
...
^C
but not helped... ;)
cd /tnp/test && rm -rf *
is safe... ;)
In cmd1 && cmd2
, cmd2
is only executed if cmd1
succeeds (returns 0).
In cmd1 ; cmd2
, cmd2
is executed in any case.
Both constructs are part of a POSIX-compliant shell.
If previous command failed with ;
the second one will run.
But with &&
the second one will not run.
This is a "lazy" logical "AND" operand between operations.