Nested if statements

If a single level of if statements is not enough and you would like to have additional logic within an if statement, you can create nested conditional statements. This can be done in the following way:

#!/bin/bashUSER_AGE=18AGE_LIMIT=18NAME="Bob" # Change to your username if you want to execute the nested logicHAS_NIGHTMARES="true"if [ "${USER}" == "${NAME}" ]; then    if [ ${USER_AGE} -ge ${AGE_LIMIT} ]; then        if [ "${HAS_NIGHTMARES}" == "true" ]; then            echo "${USER} gets nightmares, and should not see the movie"        fi    fielse    echo "Who is this?"fi

Get Bash Cookbook 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.