Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In bash, what's the diff between [[ test1 && test2 ]] vs [[ test1 ]] && [[ test2 ]]

Tags:

bash

What's the difference, if any, in the following two bash evaluations:

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

if [[ -s $file1 ]] && [[ $file1 -nt $file2 ]]; then
like image 729
prl77 Avatar asked Dec 16 '13 21:12

prl77


1 Answers

There is no difference. They're functionally identical.


An interesting aside, if you used [ instead of [[, there actually is a detectable difference cause by the order of evaluation:

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

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

In this case, the first line would print "side effect" while the second would not.

Again, however, this is only the case for [ and not for [[ ]].

like image 181
that other guy Avatar answered Oct 12 '22 05:10

that other guy