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
testcommand. Anifstatement 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.testis a command, which takes an expression and evaluates it:Since square brackets are a synonym for the
testcommand, you can then rewrite it as your original statement: