Understanding the test command
Let's now understand the test
command.
Using the test command with single brackets
Let's learn the following example to check the content or value of expressions:
$ test $name = Ganesh $ echo $? 0 if success and 1 if failure.
In the preceding example, we want to check if the content of the variable name is the same as Ganesh
and ?
To check this, we have used the test
command. The test
will store the result of the comparison in the ?
variable.
We can use the following syntax for the preceding test
command. In this case, we used [ ]
instead of the test
command. We've enclosed the expression to be evaluated in square brackets:
$ [[ $name = Ganesh ]] # Brackets replace the test command $ echo $? 0
During the evaluation of expressions by test
, we can even use wildcard expressions:
$ [[ $name = [Gg]????? ]] $ echo $? 0
Therefore, we can either use the test
command or square brackets for checking or evaluating expressions. Since word splitting will be performed...