Bash test

If statement has no ability to evaluate condition. Only with the help of test command, you can check condition.

Syntax of Bash test command

test condition

Another form of Bash test command

[ condition ]

Note: In general, we use square brackets to evaluate condition. Always remember one thing, you must provide a space after the opening square bracket and a space before the closing square bracket. If you forget to do this, you will get an error message.

When you use test command then syntax of if statement will be-

if test condition
then
   commands
fi
if [ condition ]
then
   commands
fi

There are three different set of conditions that you can evaluate using test command-

  1. Numeric comparison- To learn more about this, click Bash Numeric comparison.
  2. String comparison- To learn more about this, click Bash String comparison.
  3. File comparison- To learn more about this, click Bash File comparison.

Check out these related Bash Shell Tutorial