I am always surprised that in the folder /bin there is a [ program.

Is is what is called when we are doing something like: if [ somtething ] ?

By calling the [ program explicitly in a shell it asks for a corresponding ], and when I provide the closing bracket it seems to do nothing no matter what I insert between the brackets.

Neededless to say, the usual way about getting help about a program does not work, i.e. neither man [ nor [ --help works.

share|improve this question
    
try man expr. – Ipor Sircer 2 hours ago
1  
@IporSircer [ refers to test command though, not expr , so it should be man test – Serg 40 mins ago
    
oops. sorry! i was tired. – Ipor Sircer 19 mins ago

I am always surprised that in the folder /bin there is a [ program.

You are right to be surprised, that's the only POSIX command that doesn't respect the commands file allowed characters convention.

Is is what is called when we are doing something like: if [ somtething ] ?

Precisely but it can be used without the if too.

By calling the [ program explicitly in a shell it asks for a corresponding ], and when I provide the closing bracket it seems to do nothing no matter what I insert between the brackets.

It does the same than when used with if, i.e. it sets the exit status to 0 (true) or 1 (false) depending on what you put inside the brackets. It is the same behavior than the test command with the only difference it looks for the ending ]. See man test for details.

share|improve this answer

[ is actually more commonly known as test command. If you do man [ or man test, you will see same documentation open. Typical use of this command is simply to evaluate expressions and return their condition - true or false. It is often used in if-else if-else-fi statements, although it can be used outside of if statement to conditionally run other commands via shell's && or || operators, like so.

$ [ -e /etc/passwd  ] && echo "File exists"
File exists

$ test -e /etc/passwd && echo "File exists"
File exists

Its location depends on your system. For instance, on FreeBSD it is under /bin. On Linux ( or in my particular case , Ubuntu 16.04) it is in /usr/bin/. It's also important to note that your shell may have its own implementation of test.

It should also be noted that this command has issues, which bash's implementation (commonly known as "conditional expression" reverence with double square brackets, [[ "$USER" = "root" ]] ) seeks to resolve.

share|improve this answer
    
/usr/bin/ for Amazon Linux as well. – franklinsijo 25 mins ago

The [ command's job is to evaluate test expressions. It returns with a 0 exit status (that means true) when the expression resolves to true and something else (which means false) otherwise.

It's not that it does nothing, it's just that its outcome is to be found in its exit status. In a shell, you can find out about the exit status of the last command in $? for Bourne-like shells or $status in most other shells (fish/rc/es/csh/tcsh...).

$ [ a = a ]
$ echo "$?"
0
$ [ a = b ]
$ echo "$?"
1

In other languages like perl, the exit status is returned for instance in the return value of system():

$ perl -le 'print system("[", "a", "=", "a", "]")'
0

Note that most shells have a built-in [ command. The one in /bin would typically be executed when you use another shell or when you do things like env [ foo = bar ] or find . -exec [ -f {} ] \; -print or that perl command above...

The [ command is also known by the test name. When called as test, it doesn't require a closing ] argument.

While your system may not have a man page for [, it probably has one for test. But again, note that it would document the /bin/[ or /bin/test implementation. To know about the [ builtin in your shell, you should read the documentation for your shell instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.