I am a little bit confused on what do these operators do differently when used in bash (brackets, double brackets, parenthesis and double parenthesis).
[[ , [ , ( , ((
I have seen people use them on if statements like this :
if [[condition]]
if [condition]
if ((condition))
if (condition)
Best Answer
In Bourne-like shells, an
if
statement typically looks likeThe
then
clause is executed if the exit code of thecommand-list1
list of commands is zero. If the exit code is nonzero, then theelse
clause is executed.command-list1
can be simple or complex. It can, for example, be a sequence of one or more pipelines separated by one of the operators;
,&
,&&
,||
or newline. Theif
conditions shown below are just special cases ofcommand-list1
:if [ condition ]
[
is another name for the traditionaltest
command.[
/test
is a standard POSIX utility. All POSIX shells have it builtin (though that's not required by POSIX²). Thetest
command sets an exit code and theif
statement acts accordingly. Typical tests are whether a file exists or one number is equal to another.if [[ condition ]]
This is a new upgraded variation on
test
¹ from ksh that bash, zsh, yash, busybox sh also support. This[[ ... ]]
construct also sets an exit code and theif
statement acts accordingly. Among its extended features, it can test whether a string matches a wildcard pattern (not in busybox sh).if ((condition))
Another ksh extension that bash and zsh also support. This performs arithmetic. As the result of the arithmetic, an exit code is set and the
if
statement acts accordingly. It returns an exit code of zero (true) if the result of the arithmetic calculation is nonzero. Like[[...]]
, this form is not POSIX and therefore not portable.if (command)
This runs command in a subshell. When command completes, it sets an exit code and the
if
statement acts accordingly.A typical reason for using a subshell like this is to limit side-effects of
command
ifcommand
required variable assignments or other changes to the shell's environment. Such changes do not remain after the subshell completes.if command
command is executed and the
if
statement acts according to its exit code.¹ though not really a command but a special shell construct with its own separate syntax from that of normal command, and varying significantly between shell implementations
² POSIX does require that there be a standalone
test
and[
utilities on the system however, though in the case of[
, several Linux distributions have been known to be missing it.