Bash/sh - && 和 ; 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6152659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:34:03  来源:igfitidea点击:

Bash/sh - difference between && and ;

bashshellsyntaxmultiplatform

提问by psihodelia

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?

我通常用于;在一行中组合多个命令,但有些人更喜欢&&. 有什么区别吗?例如,cd ~; cd -并且cd ~ && cd -似乎让同样的事情。哪个版本更便携,例如,像 Android 的 shell 之类的 bash 子集会支持什么?

回答by ignar

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.

这是操作之间的“惰性”逻辑“AND”操作数。

回答by jm666

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... ;)

cd /tnp/test && rm -rf *是安全的... ;)

回答by Pascal Cuoq

In cmd1 && cmd2, cmd2is only executed if cmd1succeeds (returns 0).

cmd1 && cmd2,cmd2仅在cmd1成功时才执行(返回 0)。

In cmd1 ; cmd2, cmd2is executed in any case.

cmd1 ; cmd2,cmd2在任何情况下都会执行。

Both constructs are part of a POSIX-compliant shell.

这两种结构都是符合 POSIX 的 shell 的一部分。

回答by Fredrik Pihl

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

&&表示如果前一个命令以状态 0 退出,则执行下一个命令。相反,||如果前一个命令以不等于 0 的状态退出,则使用ie;执行总是执行。

Very useful when you need to take a particular action depending on if the previous command finished OK or not.

当您需要根据上一个命令是否完成时采取特定操作时非常有用。

回答by Dave Costa

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

使用&&,仅当第一个命令成功完成(返回退出状态 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.

这在 .bashrc 下的 bash 联机帮助页中有介绍Lists。我希望任何类 Unix 的 shell 都支持这两个操作符,但我并不特别了解 Android shell。

回答by Marcel

&&allows for conditional execution while ;always has the second command being executed.

&&允许有条件执行,同时;始终执行第二个命令。

In e.g. command1 && command2, command2will only execute when command1has terminated with exit 0, signalling all went well, while in command1 ; command2the second command will always be executed no matter what the result was of command1.

在 eg 中command1 && command2command2将仅在command1以 终止时执行exit 0,表明一切顺利,而在command1 ; command2第二个命令中,无论 的结果如何,都将始终执行command1

回答by frankc

&& is logical AND in bash. Bash has short-circuit evaluation of logical AND. This idiom is a simpler way of expressing the following:

&& 在 bash 中是逻辑与。Bash 具有逻辑 AND 的短路评估。这个习语是一种更简单的表达方式:


cmd1;rc=$?
if [ $rc -eq 0 ]; then
   cmd2
fi

Whereas the ; version is merely:

而 ; 版本仅仅是:


cmd1
cmd2