3.3. Conditional execution

Multiple commands can be placed on a single line each command separated with a conditional operator which controls execution of subsequent commands

Commands may be separated with one of three conditional operators:

With these constructs simple conditional decision processes can be built.

The test command can be used to test logical expressions and combined with the conditional operators make decisions based upon a variety of tests.

Example 3.1. Using the test command and conditional operators

This example shows how to use the test command to perform some simple textural and numeric comparisons. The full range of comparisons available can be found in Table 8, “Possible test expressions”

>[ 1 -lt 2 ] && echo "1 < 2"
1 < 2
>[ 2 -lt 1 ] && echo "2 < 1"
>[ 1 -gt 2 ] && echo "1 > 2"
>[ 2 -gt 1 ] && echo "2 > 1"
2 > 1
>[ "text" = "text" ] && echo "true"
true
>[ "text" != "text" ] || echo "true"
true
>[ "text" == "foo" ] && echo "true" || echo "false"
false
>[ "text" != "foo" ] && echo "true" || echo "false"
true
>foo="bar" ; [ -z ${foo} ] && echo "true" ||echo  "false"
false
>foo="" ; [ -z ${foo} ] && echo "true" ||echo  "false"
true
>