When writing scripts, it’s important to know how to test and validate variables. Credit: Roman Samborskyi / Shutterstock This post follows up on Part 1 by examining the many ways that you can test the value of variables – e.g., whether they equal particular strings like “yes” or “no”, if they have a numeric value, if they are null (empty), or if they are larger or smaller than some value or variable. Testing variables There are quite a few ways to test the values of variables, but the operators you need to use depend on whether you’re comparing numbers or strings. This post includes a couple scripts that run through a series of comparisons for both numbers and strings to illustrate how they work. Comparing numbers To test the value of numbers, you can use any of the tests shown in the script below (e.g., -eq tests for equality). The script asks for you to enter two numbers and then runs the series of tests. #!/bin/bash echo -n "Enter first number: " read num1 echo -n "Enter second number: " read num2 echo if [ $num1 -eq $num2 ]; then echo $num1 and $num2 are equal; fi if [ $num1 -ne $num2 ]; then echo $num1 and $num2 are not equal; fi if [ $num1 -lt $num2 ]; then echo $num1 is less than $num2; fi if [ $num1 -le $num2 ]; then echo $num1 is less than or equal $num2; fi if [ $num1 -gt $num2 ]; then echo $num1 is greater than $num2; fi if [ $num1 -ge $num2 ]; then echo $num1 is greater than or equal $num2; fi # use (( and )) for these tests if (($num1 <= $num2)); then echo $num1 is less than or equal $num2; fi if (($num1 >= $num2)); then echo $num1 is greater than or equal $num2; fi If you run this script, you can compare any two numbers and see the result for each test that passes. Here’s an example: Enter first number: 11 Enter second number: 7 11 and 7 are not equal 11 is greater than 7 11 is greater than or equal 7 11 is greater than or equal 7 The results above are somewhat redundant, but only because there is more than one way to run certain tests like greater than or equal to. Comparing strings To compare two strings in a script, you would generally use a command like this one: if [ $ans == "yes" ]; then echo "OK, let’s get going" else exit fi There are, however, quite a few tests that you can run – whether strings are the same or not and even how they relate alphabetically. The script below compares two strings in many ways. #!/bin/bash echo -n "Enter first string: " read str1 echo -n "Enter second string: " read str2 echo # run tests to see how strings relate if [ "$str1" = "$str2" ]; then echo strings are the same; fi if [ "$str1" == "$str2" ]; then echo strings are the same; fi if [ "$str1" != "$str2" ]; then echo \"$str1\" and \"$str2\" differ; fi if [ "$str1" \< "$str2" ]; then echo \"$str1\" comes first alphabetically; fi if [ "$str1" \> "$str2" ]; then echo \"$str2\" comes first alphabetically; fi if [ -z "$str1" ]; then echo string1 is null; fi if [ -z "$str2" ]; then echo string2 is null; fi if [ -n "$str1" ]; then echo string1 is not null; fi if [ -n "$str2" ]; then echo string2 is not null; fi In some of the tests above, \ characters are inserted before quotes to ensure that the strings are displayed within quote marks. Here’s a script that uses this test. #!/bin/bash echo -n "enter a year> " read year this_year=`date +%Y` # get current year using the date command if [ "$year" -lt $this_year ]; then diff=`expr $this_year - $year` echo "Wow, that was $diff year{s} ago" elif [ "$year" -gt $this_year ]; then diff=`expr $year - $this_year` echo "Only $diff year(s) to go" else echo "$year is current year" fi Notice that the script above uses if, else if and else logic instead of several independent if tests. You can also try to collect mis-entered data with commands like these that will loop until a proper year is entered. #!/bin/bash echo -n "Enter year> " read year re='^[0-9]+\$' while ! [[ $year =~ $re ]] do echo "error: Year must be numeric" echo -n "year> " read year done The examples above are fairly advanced. They set up a numeric expression that will match any string of digits. If it doesn’t match (the “!” in the script means “not”), an error will be displayed and the script will continue looping until it does. Wrap-up When writing scripts, it’s important to know how to test and validate variables. You may need to verify that required arguments are provided and that the values match what is needed. Knowing how to compose the tests is an important element of script writing. Related content how-to Compressing files using the zip command on Linux The zip command lets you compress files to preserve them or back them up, and you can require a password to extract the contents of a zip file. By Sandra Henry-Stocker May 13, 2024 4 mins Linux opinion NSA, FBI warn of email spoofing threat Email spoofing is acknowledged by experts as a very credible threat. By Sandra Henry-Stocker May 13, 2024 3 mins Linux how-to The logic of && and || on Linux These AND and OR equivalents can be used in scripts to determine next actions. By Sandra Henry-Stocker May 02, 2024 4 mins Linux how-to Using the apropos command on Linux By Sandra Henry-Stocker Apr 24, 2024 3 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe