Looking in the gnuwin32/bin directory, there is an odd-looking program file named [.exe
I couldn't find it in the documentation, gnuwin32.sourceforge.net or in a google search, so I ran it and got:
$ [
[: missing `]'
$
so I gave it ] as a parameter and got
$ [ ]
$
It didn't complain, so I assumed it was on the right track. I tried:
$ [ hello ]
again, no complaints. so I tried an arithmetic expression:
$ [ 1 + 1 ]
[: +: binary operator expected
$
I tried a bunch of different combinations, including prefix & postfix notation but nothing seemed to work. What does this thing do?
test a
==
[ a ]
It's just sugar
Edit: To clarify, that's the conditional syntax, e.g. [ "a" = "a" ]
It's used to evaluate conditional expressions.
It is equivalent to (possibly a symlink to?) the test executable.
The manpage is here.
You may see this in a lot of bash scripts:
if [ "$LOGNAME" = "scott" ]
then
echo "Logged in as Scott"
else
echo "incorrect user"
fi
The funny thing is, the [ is not part of the bash language, it's actually an executable whose return code is used by the 'IF'. This is the reason why the space after the [ and its first argument is mandatory - if it would be omitted, the script would try to execute ["$LOGNAME" and fail.
You can't do arithmetical operations with it - use expr for that (see here).
However, you can test for a wide range of file properties (does it exist? what type is it? etc) as well as use comparison operators on strings and numbers.
Another answer already mentioned it is the same as test. On bash, it is also a builtin, so you can get the help for it with the help builtin (help test).
Related
I was trying to write a Bash script that uses an if statement.
if[$CHOICE -eq 1];
The script was giving me errors until I gave a space before and after [ and before ] as shown below:
if [ $CHOICE -eq 1 ];
My question here is, why is the space around the square brackets so important in Bash?
Once you grasp that [ is a command, a whole lot becomes clearer!
[ is another way to spell "test".
help [
However while they do exactly the same, test turns out to have a more detailed help page. Check
help test
...for more information.
Furthermore note that I'm using, by intention, help test and not man test. That's because test and [ are shell builtin commands nowadays. Their feature set might differ from /bin/test and /bin/[ from coreutils which are the commands described in the man pages.
From another question:
A bit of history: this is because '[' was historically not a shell-built-in but a separate executable that received the expresson as arguments and returned a result. If you didn't surround the '[' with space, the shell would be searching $PATH for a different filename (and not find it) . – Andrew Medico Jun 24 '09 at 1:13
[ is a command and $CHOICE should be an argument, but by doing [$CHOICE (without any space between [ and $CHOICE) you are trying to run a command named [$CHOICE. The syntax for command is:
command arguments separated with space
[ is a test command. So it requires space.
It's worth noting that [ is also used in glob matching, which can get you into trouble.
$ echo [12345]
[12345]
$ echo oops >3
$ echo [12345]
3
I was trying to write a Bash script that uses an if statement.
if[$CHOICE -eq 1];
The script was giving me errors until I gave a space before and after [ and before ] as shown below:
if [ $CHOICE -eq 1 ];
My question here is, why is the space around the square brackets so important in Bash?
Once you grasp that [ is a command, a whole lot becomes clearer!
[ is another way to spell "test".
help [
However while they do exactly the same, test turns out to have a more detailed help page. Check
help test
...for more information.
Furthermore note that I'm using, by intention, help test and not man test. That's because test and [ are shell builtin commands nowadays. Their feature set might differ from /bin/test and /bin/[ from coreutils which are the commands described in the man pages.
From another question:
A bit of history: this is because '[' was historically not a shell-built-in but a separate executable that received the expresson as arguments and returned a result. If you didn't surround the '[' with space, the shell would be searching $PATH for a different filename (and not find it) . – Andrew Medico Jun 24 '09 at 1:13
[ is a command and $CHOICE should be an argument, but by doing [$CHOICE (without any space between [ and $CHOICE) you are trying to run a command named [$CHOICE. The syntax for command is:
command arguments separated with space
[ is a test command. So it requires space.
It's worth noting that [ is also used in glob matching, which can get you into trouble.
$ echo [12345]
[12345]
$ echo oops >3
$ echo [12345]
3
I was trying to write a Bash script that uses an if statement.
if[$CHOICE -eq 1];
The script was giving me errors until I gave a space before and after [ and before ] as shown below:
if [ $CHOICE -eq 1 ];
My question here is, why is the space around the square brackets so important in Bash?
Once you grasp that [ is a command, a whole lot becomes clearer!
[ is another way to spell "test".
help [
However while they do exactly the same, test turns out to have a more detailed help page. Check
help test
...for more information.
Furthermore note that I'm using, by intention, help test and not man test. That's because test and [ are shell builtin commands nowadays. Their feature set might differ from /bin/test and /bin/[ from coreutils which are the commands described in the man pages.
From another question:
A bit of history: this is because '[' was historically not a shell-built-in but a separate executable that received the expresson as arguments and returned a result. If you didn't surround the '[' with space, the shell would be searching $PATH for a different filename (and not find it) . – Andrew Medico Jun 24 '09 at 1:13
[ is a command and $CHOICE should be an argument, but by doing [$CHOICE (without any space between [ and $CHOICE) you are trying to run a command named [$CHOICE. The syntax for command is:
command arguments separated with space
[ is a test command. So it requires space.
It's worth noting that [ is also used in glob matching, which can get you into trouble.
$ echo [12345]
[12345]
$ echo oops >3
$ echo [12345]
3
I was trying to write a Bash script that uses an if statement.
if[$CHOICE -eq 1];
The script was giving me errors until I gave a space before and after [ and before ] as shown below:
if [ $CHOICE -eq 1 ];
My question here is, why is the space around the square brackets so important in Bash?
Once you grasp that [ is a command, a whole lot becomes clearer!
[ is another way to spell "test".
help [
However while they do exactly the same, test turns out to have a more detailed help page. Check
help test
...for more information.
Furthermore note that I'm using, by intention, help test and not man test. That's because test and [ are shell builtin commands nowadays. Their feature set might differ from /bin/test and /bin/[ from coreutils which are the commands described in the man pages.
From another question:
A bit of history: this is because '[' was historically not a shell-built-in but a separate executable that received the expresson as arguments and returned a result. If you didn't surround the '[' with space, the shell would be searching $PATH for a different filename (and not find it) . – Andrew Medico Jun 24 '09 at 1:13
[ is a command and $CHOICE should be an argument, but by doing [$CHOICE (without any space between [ and $CHOICE) you are trying to run a command named [$CHOICE. The syntax for command is:
command arguments separated with space
[ is a test command. So it requires space.
It's worth noting that [ is also used in glob matching, which can get you into trouble.
$ echo [12345]
[12345]
$ echo oops >3
$ echo [12345]
3
I was trying to write a Bash script that uses an if statement.
if[$CHOICE -eq 1];
The script was giving me errors until I gave a space before and after [ and before ] as shown below:
if [ $CHOICE -eq 1 ];
My question here is, why is the space around the square brackets so important in Bash?
Once you grasp that [ is a command, a whole lot becomes clearer!
[ is another way to spell "test".
help [
However while they do exactly the same, test turns out to have a more detailed help page. Check
help test
...for more information.
Furthermore note that I'm using, by intention, help test and not man test. That's because test and [ are shell builtin commands nowadays. Their feature set might differ from /bin/test and /bin/[ from coreutils which are the commands described in the man pages.
From another question:
A bit of history: this is because '[' was historically not a shell-built-in but a separate executable that received the expresson as arguments and returned a result. If you didn't surround the '[' with space, the shell would be searching $PATH for a different filename (and not find it) . – Andrew Medico Jun 24 '09 at 1:13
[ is a command and $CHOICE should be an argument, but by doing [$CHOICE (without any space between [ and $CHOICE) you are trying to run a command named [$CHOICE. The syntax for command is:
command arguments separated with space
[ is a test command. So it requires space.
It's worth noting that [ is also used in glob matching, which can get you into trouble.
$ echo [12345]
[12345]
$ echo oops >3
$ echo [12345]
3