在Linux Bash中,双与号(&&)和分号(;)之间有什么区别? [英] What is the difference between double-ampersand (&&) and semicolon (;) in Linux Bash?

查看:226
本文介绍了在Linux Bash中,双与号(&&)和分号(;)之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linux Bash 中的&符和分号有什么区别? >

例如,

$ command1 && command2

vs

$ command1; command2

解决方案

&&运算符是布尔AND运算符:如果左侧返回非零退出状态,则该运算符将返回该状态并且不评估右侧(短路),否则评估右侧并返回其退出状态.这通常用于确保command2仅在command1成功运行时才运行.

;令牌只是分隔命令,因此无论第一个命令是否成功,它都会运行第二个命令.

What is the difference between ampersand and semicolon in Linux Bash?

For example,

$ command1 && command2

vs

$ command1; command2

解决方案

The && operator is a boolean AND operator: if the left side returns a non-zero exit status, the operator returns that status and does not evaluate the right side (it short-circuits), otherwise it evaluates the right side and returns its exit status. This is commonly used to make sure that command2 is only run if command1 ran successfully.

The ; token just separates commands, so it will run the second command regardless of whether or not the first one succeeds.

这篇关于在Linux Bash中,双与号(&&)和分号(;)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆