Usually, I use square brackets in the if statement:
if [ "$name" = 'Bob' ]; then ...
But, when I check if grep
succeeded I don't use the square brackets:
if grep -q "$text" $file ; then ...
When are the square brackets necessary in the if
statement?
The square brackets are a synonym for the
test
command. Anif
statement checks the exit status of a command in order to decide which branch to take.grep -q "$text"
is a command, but"$name" = 'Bob'
is not--it's just an expression.test
is a command, which takes an expression and evaluates it:Since square brackets are a synonym for the
test
command, you can then rewrite it as your original statement:The best way to think of the
[ ... ]
syntax, is to consider[
to be a program - which it is!Check this out:
on the other hand, you're probably not using that version of it since
bash
also provides[
as a shell built-in.Anyway, to answer your question: What
if
does is run the command you give it and see it the return value is0
or not. You use[
to do other, more interesting comparisons such as string comparisons. Seeman [
andman bash
.[
is actually a command, equivalent (almost, see below) to thetest
command. It's not part of the shell syntax. (Both[
andtest
, depending on the shell, are often built-in commands as well, but that doesn't affect their behavior, except perhaps for performance.)An
if
statement executes a command and executes thethen
part if the command succeeds, or theelse
part (if any) if it fails. (A command succeeds if it exits with a status ($?
) of 0, fails if it exits with a non-zero status.)In
the command is
(You could execute that same command directly, without the
if
.)In
the command is
man [
orman test
for more information.FOOTNOTE: Well, the
[
command is almost equivalent to thetest
command. The difference is that[
requires]
as its last argument, andtest
does not -- and in fact doesn't allow it. (It didn't have to be implemented that way, but a[
without a matching]
would have made a lot of people very very nervous.)