I normally use ;
to combine more than one command in a line, but some people prefer &&
. Is there any difference? For example, cd ~; cd -
and cd ~ && cd -
seems to make the same thing. What version is more portable, e.g. will be supported by a bash-subset like Android's shell or so?

- 382,024
- 64
- 607
- 775

- 29,566
- 35
- 108
- 157
7 Answers
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.
-
4In other words it's got a short-circuit behaviour – Peter Chaula Jul 12 '18 at 08:49
-
1@peter Which one? The `;` one or the `&&` one? Or do you mean both? – Shayan Aug 29 '19 at 18:55
-
@Shayan, the `&&`. – Peter Chaula Aug 29 '19 at 19:06
-
3So you would prefer to run `sudo apt update && sudo apt upgrade` and not `sudo apt update; sudo apt upgrade`, right? – Vthechamp Feb 03 '21 at 16:38
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... ;)

- 12,032
- 6
- 60
- 71

- 62,119
- 17
- 107
- 194
-
35
-
18To be even safer it is better to use `rm /tnp/test/ -rf` to prevent deadly enter missclick – radrow Jun 01 '18 at 12:49
-
1
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.

- 79,187
- 7
- 161
- 281
&&
means to execute next command if the previous exited with status 0.
For the opposite, use ||
i.e. to be executed if previous command exits with a status not equal to 0 ;
executes always.
Very useful when you need to take a particular action depending on if the previous command finished OK or not.

- 44,604
- 7
- 83
- 130
-
Note that if you combine `&&` and `||`, then you must put the `&&` first. Otherwise, the `&&` executes if **either** the original or the `||` command succeeds (exits with 0 status). – Scott H Oct 15 '21 at 19:29
Commands separate by ;
are executed sequentially regardless of their completion status.
With &&
, the second command is executed only if the first completes successfully (returns exit status of 0).
This is covered in the bash manpage under Lists
. I would expect any Unix-like shell to support both of these operators, but I don't know specifically about the Android shell.

- 47,262
- 8
- 56
- 72
-
2Here's a link to the bash manual: http://www.gnu.org/software/bash/manual/bashref.html#Lists – glenn jackman May 27 '11 at 14:05
&&
allows for conditional execution while ;
always has the second command being executed.
In e.g. command1 && command2
, command2
will only execute when command1
has terminated with exit 0
, signalling all went well, while in command1 ; command2
the second command will always be executed no matter what the result was of command1
.
&& is logical AND in bash. Bash has short-circuit evaluation of logical AND. This idiom is a simpler way of expressing the following:
cmd1;rc=$?
if [ $rc -eq 0 ]; then
cmd2
fi
Whereas the ; version is merely:
cmd1
cmd2

- 11,290
- 4
- 32
- 49