What do square brackets mean without the "if" on the left?
Square brackets are a shorthand notation for performing a conditional test. The brackets [
, as well as [[
are actual commands within Unix, believe it or not.
Think:
$ [ -f /etc/rc.local ] && echo "real file"
real file
-and-
$ test -f /etc/rc.local && echo "real file"
real file
In Bash the [
is a builtin command as well as an executable. [[
is just a keyword to Bash.
Example
You can confirm this using type
:
$ type -a [
[ is a shell builtin
[ is /usr/bin/[
$ type -a [[
[[ is a shell keyword
You can see the physical executable here:
$ ls -l /usr/bin/[
-rwxr-xr-x 1 root root 37000 Nov 3 2010 /usr/bin/[
builtins vs. keywords
If you take a look at the Bash man page, man bash
, you'll find the following definitions for the 2:
keywords - Reserved words are words that have a special meaning to the shell. The following words are recognized as reserved when unquoted and either the first word of a simple command (see SHELL GRAMMAR below) or the third word of a case or for command:
! case do done elif else esac fi for function if in select then until while { } time [[ ]]
builtins - If the command name contains no slashes, the shell attempts to locate it. If there exists a shell function by that name, that function is invoked as described above in FUNCTIONS. If the name does not match a function, the shell searches for it in the list of shell builtins. If a match is found, that builtin is invoked.
If the name is neither a shell function nor a builtin, and contains no slashes, bash searches each element of the PATH for a directory containing an executable file by that name. Bash uses a hash table to remember the full pathnames of executable files (see hash under SHELL BUILTIN COMMANDS below). A full search of the directories in PATH is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle. If that function exists, it is invoked with the original command and the original command's arguments as its arguments, and the function's exit status becomes the exit status of the shell. If that function is not defined, the shell prints an error message and returns an exit status of 127.
man page
If you look through the Bash man page you'll find the details on it.
test expr
[ expr ]
Return a status of 0 or 1 depending on the evaluation of the
conditional expression expr. Each operator and operand must be
a separate argument. Expressions are composed of the primaries
described above under CONDITIONAL EXPRESSIONS. test does not
accept any options, nor does it accept and ignore an argument of
-- as signifying the end of options.
Lastly from the man page:
test and [ evaluate conditional expressions using a set of rules
based on the number of arguments.
EDIT #1
Follow-up question from the OP.
Ok, so why is there a need for an "if" then? I mean, why "if" even exists if "[" would suffice.
The if
is part of a conditional. The test
command or [ ... ]
command simply evaluate the conditional, and return a 0 or a 1. The 0 or 1 is then acted on by the if statement. The 2 are working together when you use them.
Example
if [ ... ]; then
... do this ...
else
... do that ...
fi
Ooohh, one of my favorite topics!!
Square brackets are a synonym for the "test" command. If you read the test man page, you'll see that you can invoke the test command as either
test -r /etc/profile.d/java.sh
or
[ -r /etc/profile.d/java.sh ]
The spaces between the brackets and the stuff inside and outside them are required.
The "test" command in this case is checking to see if the file /etc/profile.d/java.sh is readable to the current user. Implied is a check to see if it exists, of course. :-)
The &&
is a bash syntax shortcut for "if the command on the left succeeds, then execute the command on the right. So, this compound command is a shorthand for an "if-then" that would look like this:
if test -r /etc/profile.d/java.sh
then
/etc/profile.d/java.sh
fi
Now, you'll also find double square brackets explained in the bash man page. Those are a bash internal version of an extended testing function. Be aware that those are not exactly the same. There are things you can do with those that you cannot do with the "test" command and its "[" synonym.