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 How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe