When should i use [] or [[]]?

As the title states, when should I use [] or [[]] when writing an if statement or something like that, i am not quite sure where the difference lies, and when i should use the first from the latter one?

Hi kidi...

Take a look at:-

Test Constructs

Single 'test' brackets are portable and POSIX compliant.
Double 'test' brackets are an advanced shell variant, 'bash' for example, usage only at this point in time.

1 Like

FWIW - this shows [[ as part of the POSIX shell
IBM Knowledge Center Error

Because Shell Command Language
in the "Case Conditional Construct" section shows the function of [[ ]]

So it appears any POSIX conforming shell will support [[

The POSIX-conforming shell on AIX is the Korn shell and the Korn shell includes the keyword [[ . So, I assume that you can use [[ in AIX's sh which is based on or linked to ksh .

The text from the POSIX shell command language you pointed to above says:

So, the POSIX standard allows [[ expression ]] to be supported by conforming shells, but says absolutely nothing about what [[ expression ]] does in a shell script.

The next revision of the POSIX standards and the Single UNIX Specification (which are likely to be balloted by IEEE, ISO/IEC, and The Open Group in about 3-7 years) is, in my personal opinion, likely to add defined behavior for the keywords [[ and ]] and many, or all, of common set of expressions that are available in both recent bash and ksh93 shells with [[ expression ]] . You can see a very lengthy record of a small subset of the discussion on this topic in The Austin Group's bug report on this topic: Bug #375: Extend test/[...] conditionals: ==, <, >, -nt, -ot, -ef. Note that this bug report is still open AND that the initially suggested Desired Action will NOT make it into the standard.

From Shell Check:-

#!/bin/sh
var="OK"
if [[ "$var" = "OK" ]]
then
        echo "Hello Wolrd."
fi

Error report from same:-

$ shellcheck myscript
 
Line 3:
if [[ "$var" = "OK" ]]
   ^-- SC2039: In POSIX sh, [[ ]] is undefined.

$ 

The [ ] and [[ ]] are effectively similar.
The equal test is = in [ ] and == in [[ ]] (while some shells allow the other operator, too).
The == and != in [[ ]] allow matches with shell wildcards. Not possible with [ ].
The reason is that [ is a command, and all following tokens are command arguments where the shell does expansion and substitutions before passing them to the [ command; the last argument is the ].
In fact, there is the equivalent test command: test a b c is the same as [ a b c ] .
In contrast, [[ and ]] are reserved words in the shell syntax.

function is a reserved word too, but that does not mean to say it is in use yet.
As usual OSX 10.7.5, default bash terminal initially calling dash and running a piece of code, then exiting and doing the same in the default bash shell...

Last login: Mon Aug 29 08:10:45 on console
AMIGA:barrywalker~> dash
AMIGA:\u\w> function text() { echo "Does this work in dash?"; }       
dash: 1: Syntax error: "(" unexpected
AMIGA:\u\w> function text { echo ""; }
dash: 1: Syntax error: "}" unexpected
AMIGA:\u\w> function { echo "" ;
dash: 6: function: not found
AMIGA:\u\w> exit
AMIGA:barrywalker~> function text() { echo "Does this work in dash?"; }
AMIGA:barrywalker~> text
Does this work in dash?
AMIGA:barrywalker~> _

EDIT:-
See below...

Hi,
For my part, with bash, i use [[ ]] for regular expression (operator '=~' ) that not work with [ ].

Regards.