What is the difference between double-ampersand (&&) and semicolon (;) in Linux Bash?

前端 未结 3 1835
盖世英雄少女心
盖世英雄少女心 2020-12-07 10:03

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

For example,

$ command1 && command2

vs



        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 11:01

    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.

提交回复
热议问题