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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers When should i use [] or [[]]?
# 1  
Old 08-27-2016
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?
# 2  
Old 08-27-2016
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.
This User Gave Thanks to wisecracker For This Post:
# 3  
Old 08-28-2016
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 [[
# 4  
Old 08-28-2016
Quote:
Originally Posted by jim mcnamara
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:
Quote:
The following words may be recognized as reserved words on some implementations (when none of the characters are quoted), causing unspecified results:
[[ ]] function select
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.

Last edited by Don Cragun; 08-28-2016 at 01:21 PM.. Reason: Add link to POSIX Bug #375.
# 5  
Old 08-28-2016
From Shell Check:-
Code:
#!/bin/sh
var="OK"
if [[ "$var" = "OK" ]]
then
        echo "Hello Wolrd."
fi

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

$

# 6  
Old 08-29-2016
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.
# 7  
Old 08-29-2016
Quote:
Originally Posted by MadeInGermany
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...
Code:
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...

Last edited by wisecracker; 08-29-2016 at 04:47 AM.. Reason: Added line to show 'function not found'.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question