Command grouping (&&, ||, …)

前端 未结 2 2009
盖世英雄少女心
盖世英雄少女心 2020-12-29 11:32

We are currently in the /home/student/ directory. We execute the following commands:

pwd; (ls) || { cd .. && ls student/; }  && cd student ||         


        
2条回答
  •  自闭症患者
    2020-12-29 11:48

    (ls) is executed in a subshell, because the commands are seperated with ";"

    ls is executed in a subshell because of the parentheses. Parentheses introduce subshells.

    But I have no idea why other commands are executed?

    Unlike other programming languages you might be familiar with, bash does not give && higher precedence than ||. They have equal precedence and are evaluated from left to right.

    If you had a || b && c, in other languages this would be read as a || {b && c;}. If a were true then neither b nor c would be evaluated.

    In bash, though, it's parsed as {a || b;} && c (strict left-to-right precedence), so when a is true b is skipped but c is still evaluated.

提交回复
热议问题