Comparisons and tests

Flow control in a program is handled by comparison and test statements. Bash also comes with several options to perform tests that are compatible with the Unix system-level features. We can use if, if else, and logical operators to perform tests and certain comparison operators to compare data items. There is also a command called test available to perform tests. Let us see how to use these.

How to do it...

We will have a look at all the different methods used for comparisons and performing tests:

  • Using an if condition:
    if condition;
    then
        commands;
    fi
  • Using else if and else:
    if condition; 
    then
        commands;
    else if condition; then
        commands;
    else
        commands;
    fi

    Note

    Nesting is also possible with if and else. The if conditions can be lengthy, ...

Get Linux Shell Scripting Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.