Evaluating strings

As mentioned in the variables subsection, numeric values are different from strings. Strings are typically evaluated like this:

#!/bin/bashMY_NAME="John"NAME_1="Bob"NAME_2="Jane"NAME_3="Sue"Name_4="Kate"if [ "${MY_NAME}" == "Ron" ]; then    echo "Ron is home from vacation"elif [ "${MY_NAME}" != ${NAME_1}" && "${MY_NAME}" != ${NAME_2}" && "${MY_NAME}" == "John" ]; then    echo "John is home after some unnecessary AND logic"elif [ "${MY_NAME}" == ${NAME_3}" || "${MY_NAME}" == ${NAME_4}" ]; then    echo "Looks like one of the ladies are home"else    echo "Who is this stranger?"fi

In the preceding snippet, you might notice that the MY_NAME variable will be executed and the string John is home after some unnecessary AND logic will be echoed ...

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.