在bash中,[[test1 && test2]]与[[test1]] && [[test2]]之间的区别是什么?

prl*_*l77 8 bash

在以下两个bash评估中有什么区别(如果有的话):

if [[ -s $file1 && $file1 -nt $file2 ]]; then

if [[ -s $file1 ]] && [[ $file1 -nt $file2 ]]; then
Run Code Online (Sandbox Code Playgroud)

tha*_*guy 5

没有区别.它们在功能上是相同的.


有趣的是,如果您使用[而不是[[,实际上由评估顺序引起的可检测差异:

[ -s "$file1" -a "$file1" -nt "$(echo side effect >&2)" ] 

[ -s "$file1" ] && [ "$file1" -nt "$(echo side effect >&2)" ] 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,第一行将打印"副作用"而第二行则不会.

但是,这只是个案,[而不是[[ ]].