vendredi 1 juillet 2016

Simplified bash conditional statements - curly brackets vs parenthesis? [duplicate]

This question already has an answer here:

I do a lot of scripting in Bash, and instead of using the typical syntax:

if [[ condition ]]
then 
    foo
    bar
else 
    baz
    quux
fi

I often use a simplified version of it:

[[ condition ]] && ( foo; bar ) || ( baz; quux )

However, looking at some scripts at the new company I'm working at, I noticed that in place of the regular parenthesis, it works just fine using the curly brackets. For example:

$ false && ( echo -n "Hello "; echo "World"; ) || ( echo -n "Goodbye "; echo "World";  )
Goodbye World
$ true && ( echo -n "Hello "; echo "World"; ) || ( echo -n "Goodbye "; echo "World";  )
Hello World
$ false && { echo -n "Hello "; echo "World"; } || { echo -n "Goodbye "; echo "World";  }
Goodbye World
$ true && { echo -n "Hello "; echo "World"; } || { echo -n "Goodbye "; echo "World";  }
Hello World

So the first two examples use parenthesis, which is what I usually use. The last two use curly brackets.

So my question is... whats the difference? When should I use one and not the other?

Thanks!

Aucun commentaire:

Enregistrer un commentaire