Difference between test, [..] and [[...]]

  • Follow


Hello Everybody,

I am new to Unix. Oflate i have been analyzing lot of shell scripts...
but couldn't find the difference between the following commands.

1) if  test "expr"; then do something; else do something else; fi

2) if [ "expr" ]; then do something; else do something else; fi

3) if [[ "expr" ]]; then do something; else do something else; fi


Can any one brief me on this thing. Thankyou.

0
Reply reddy.preetham (1) 3/6/2006 1:53:51 PM

Preetham Reddy wrote:
> Hello Everybody,
>
> I am new to Unix. Oflate i have been analyzing lot of shell scripts...
> but couldn't find the difference between the following commands.
>
> 1) if  test "expr"; then do something; else do something else; fi
>
> 2) if [ "expr" ]; then do something; else do something else; fi
>
> 3) if [[ "expr" ]]; then do something; else do something else; fi
>
>
> Can any one brief me on this thing. Thankyou.

Nice resource for scripting in general:
  http://www.tldp.org/LDP/abs/html/index.html

and for your question in particular:
  http://www.tldp.org/LDP/abs/html/testconstructs.html

0
Reply shakahshakah 3/6/2006 3:14:18 PM


On 2006-03-06, Preetham Reddy wrote:
> Hello Everybody,
>
> I am new to Unix. Oflate i have been analyzing lot of shell scripts...
> but couldn't find the difference between the following commands.
>
> 1) if  test "expr"; then do something; else do something else; fi
>
> 2) if [ "expr" ]; then do something; else do something else; fi
>
> 3) if [[ "expr" ]]; then do something; else do something else; fi

   1 and 2 are the same ('[' is the same as 'test').

   3 is a non-portable syntax found in bash and ksh. The example you
   gave has the same effect as 1 and 2, but '[[..]]' has more features
   than 'test'. They are mostly available through other (standard)
   syntax such as 'case'.

-- 
   Chris F.A. Johnson, author   |    <http://cfaj.freeshell.org>
   Shell Scripting Recipes:     |  My code in this post, if any,
   A Problem-Solution Approach  |          is released under the
   2005, Apress                 |     GNU General Public Licence
0
Reply Chris 3/8/2006 10:17:15 AM

2 Replies
474 Views

(page loaded in 0.005 seconds)

Similiar Articles:













7/25/2012 8:15:14 AM


Reply: