LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is command test equivalent to "[]"? (https://www.linuxquestions.org/questions/linux-newbie-8/is-command-test-equivalent-to-%5B%5D-930894/)

johnifanx98 02-23-2012 10:52 AM

Is command test equivalent to "[]"?
 
I see example like below, which I thought should be used with test.

Code:

[ $retval -eq 0 ] && echo "$user found" || echo "$user not found"
I checked man test, and it hints test is equivalent to [].

SYNOPSIS
test EXPRESSION
test

[ EXPRESSION ]
[ ]
[ OPTION

grail 02-23-2012 11:04 AM

And what happens when you do - man [

johnifanx98 02-23-2012 11:09 AM

Quote:

Originally Posted by grail (Post 4610233)
And what happens when you do - man [

Code:

NAME
      test - check file types and compare values

SYNOPSIS
      test EXPRESSION
      test

      [ EXPRESSION ]
      [ ]
      [ OPTION


catkin 02-23-2012 11:22 AM

To get the help for bash built-ins, use help test etc. The man pages give information about the executables that bash uses as "externals". Mostly they work the same but not always.

David the H. 02-23-2012 11:26 AM

Try reading info test too.

But catkin is right. bash has it's own internal implementation of test.

And while you're there, also take a look at [[ and ((..)).

johnifanx98 02-23-2012 11:37 AM

Quote:

Originally Posted by catkin (Post 4610242)
To get the help for bash built-ins, use help test etc. The man pages give information about the executables that bash uses as "externals". Mostly they work the same but not always.

Thanks a million. Great to know how to check syntax of built-in commands...

johnifanx98 02-23-2012 11:42 AM

Quote:

Originally Posted by David the H. (Post 4610247)
Try reading info test too.

But catkin is right. bash has it's own internal implementation of test.

And while you're there, also take a look at [[ and ((..)).

Thank you! It looks like in terms of arguments and return, test and [[ are exchangeable. That pretty answers my question

catkin 02-23-2012 11:46 AM

Quote:

Originally Posted by johnifanx98 (Post 4610262)
Thank you! It looks like in terms of arguments and return, test and [[ are exchangeable. That pretty answers my question

Not exactly. test and [ ... ] are interchangeable. [[ ... ]] is the souped-up GT-racing version.

David the H. 02-26-2012 10:50 AM

See here for the differences between [ and [[.

http://mywiki.wooledge.org/BashFAQ/031

((..)) is one technique for arithmetic evaluation, which can be used instead of the regular tests when working with integers.

http://mywiki.wooledge.org/ArithmeticExpression


All times are GMT -5. The time now is 08:11 AM.