Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash/sh - difference between && and ;

People also ask

What is the difference between bin bash and bin sh?

Basically bash is sh, with more features and better syntax. Most commands work the same, but they are different. Bash (bash) is one of many available (yet the most commonly used) Unix shells. Bash stands for "Bourne Again SHell",and is a replacement/improvement of the original Bourne shell (sh).

What is the difference between bash and Linux?

bash is one shell. Technically Linux is not a shell but in fact the kernel, but many different shells can run on top of it (bash, tcsh, pdksh, etc.). bash just happens to be the most common one.

Should I use bin sh or bin bash?

On different systems, /bin/sh might be a link to ash, bash, dash, ksh, zsh, etc. It will always be sh-compatible though, never csh or fish. As long as you stick to sh features only, you can (and probably even should) use #!/bin/sh and the script should work fine, no matter which shell it is.

What is sh command in bash?

Description. The sh command invokes the default shell and uses its syntax and flags. The shell linked to the /usr/bin/sh path is the default shell. The standard configuration of the operating system links the /usr/bin/sh path to the Korn 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.


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.


&& 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.


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.


&& 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