What is the purpose of “&&” in a shell command?

2019-01-02 16:22发布

As far as I know, using & after the command is for running it in the background.

Example of & usage: tar -czf file.tar.gz dirname &

But how about &&? (look at this example: https://serverfault.com/questions/215179/centos-100-disk-full-how-to-remove-log-files-history-etc#answer-215188)

8条回答
深知你不懂我心
2楼-- · 2019-01-02 16:59

&& lets you do something based on whether the previous command completed successfully - that's why you tend to see it chained as do_something && do_something_else_that_depended_on_something.

查看更多
柔情千种
3楼-- · 2019-01-02 17:02

command-line - what is the purpose of &&?

In shell, when you see

$ command one && command two

the intent is to execute the command that follows the && only if the first command is successful. This is idiomatic of Posix shells, and not only found in Bash.

It intends to prevent the running of the second process if the first fails.

Processes return 0 for true, other positive numbers for false

Programs return a signal on exiting. They should return 0 if they exit successfully, or greater than zero if they do not. This allows a limited amount of communication between processes.

The && is referred to as AND_IF in the posix shell grammar, which is part of an and_or list of commands, which also include the || which is an OR_IF with similar semantics.

Grammar symbols:

%token  AND_IF    OR_IF    DSEMI
/*      '&&'      '||'     ';;'    */

Grammar:

and_or           :                         pipeline
                 | and_or AND_IF linebreak pipeline
                 | and_or OR_IF  linebreak pipeline

Both operators have equal precedence and are evaluated left to right (they are left associative) For example, the following:

$ false && echo foo || echo bar
$ true || echo foo && echo bar

both echo only bar.

  1. In the first case, the false is a command that exits with the status of 1

    $ false
    $ echo $?
    1
    

    which means echo foo does not run (i.e., shortcircuiting echo foo). Then the command echo bar is executed.

  2. In the second case, true exits with a code of 0

    $ true
    $ echo $?
    0
    

    and therefore echo foo is not executed, then echo bar is executed.

查看更多
后来的你喜欢了谁
4楼-- · 2019-01-02 17:06

Furthermore, you also have || which is the logical or, and also ; which is just a separator which doesn't care what happend to the command before.

$ false || echo "Oops, fail"
Oops, fail

$ true || echo "Will not be printed"
$  

$ true && echo "Things went well"
Things went well

$ false ; echo "This will always run"
This will always run

Some details about this can be found here Lists of Commands in the Bash Manual.

查看更多
笑指拈花
5楼-- · 2019-01-02 17:06

A quite common usage for '&&' is compiling software with autotools. For example:

./configure --prefix=/usr && make && sudo make install

Basically if the configure succeeds, make is run to compile, and if that succeeds, make is run as root to install the program. I use this when I am mostly sure that things will work, and it allows me to do other important things like look at stackoverflow an not 'monitor' the progress.

Sometimes I get really carried away...

tar xf package.tar.gz && ( cd package; ./configure && make && sudo make install ) && rm package -rf

I do this when for example making a linux from scratch box.

查看更多
素衣白纱
6楼-- · 2019-01-02 17:09

See the example:

mkdir test && echo "Something" > test/file

Shell will try to create directory test and then, only if it was successfull will try create file inside it.

So you may interrupt a sequence of steps if one of them failed.

查看更多
不再属于我。
7楼-- · 2019-01-02 17:13

&& strings commands together. Successive commands only execute if preceding ones succeed.

Similarly, || will allow the successive command to execute if the preceding fails.

See Bash Shell Programming.

查看更多
登录 后发表回答