We are currently in the /home/student/ directory. We execute the following commands:
pwd; (ls) || { cd .. && ls student/; } && cd student || cd / && cd ;
The commands that are executed are: pwd, ls, cd student, cd /, cd
Here is what I think:
pwd is executed, because it's the first command
(ls) is executed in a subshell, because the commands are seperated
with ";"
So far everything clear, I guess. But I have no idea why other commands are executed? If someone could break it down for me, I'd appreciate it.
Operator precedence for &&
and ||
is strictly left-to-right.
Thus:
pwd; (ls) || { cd .. && ls student/; } && cd student || cd / && cd ;
...is equivalent to...
pwd; { { { (ls) || { cd .. && ls student/; }; } && cd student; } || cd /; } && cd ; }
...breaking that down graphically:
pwd; { # 1
{ # 2
{ (ls) || # 3
{ cd .. && # 4
ls student/; # 5
}; # 6
} && cd student; # 7
} || cd /; # 8
} && cd ; # 9
pwd
happens unconditionallyls
happens (in a subshell) unconditionally.cd ..
happens if (3) failed.ls student/
happens if (3) failed and (4) succeededcd student
happens if either (3) succeeded or both (4) and (5) succeeded.cd /
happens if either [both (3) and one of (4) or (5) failed], or [(7) failed].cd
happens if (7) occurred and succeeded, or (8) occurred and succeeded.Using explicit grouping operators is wise to avoid confusing yourself. Avoiding writing code as hard to read as this is even wiser.