&&
is a logical operator. ;
is simple sequencing.
In cmd1 && cmd2
, cmd2 will only be run if cmd1 exits with a successful return code.
Whereas in cmd1; cmd2
, cmd2 will run regardless of the exit status of cmd1 (assuming you haven't set your shell to exit on all failure in your script or something).
On a related note, with cmd1 || cmd2
, using the ||
'OR' logical operator, cmd2 will only be run if cmd1 fails (returns a non-zero exit code).
These logical operators are sometimes used in scripts in place of a basic if statement. For example,
if [[ -f "$foo" ]]; then mv "$foo" "${foo%.txt}.mkd"; fi
...can be more concisely achieved with:
[[ -f "$foo" ]] && mv "$foo" "${foo%.txt}.mkd"
2Win command-line and batch have the same feature:
& (simple sequencing), && (conditional AND) and || (conditional OR)
. – Karan – 2013-07-12T16:13:30.7535
See also: Bash Reference Manual – List of commands. In general, there's nothing you can't find in the documentation; it's really worth looking at if you have a question about specific syntax elements.
– slhck – 2013-07-12T17:51:20.3871@Karan And for completeness, bash (linux/Ubuntu) has
||
as well. – Izkata – 2013-07-12T19:09:09.857