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 examine files on Linux Linux provides very useful options for viewing file attributes, such as owners and permissions, as well as file content. By Sandra Henry Stocker Oct 24, 2024 6 mins Linux how-to 8 easy ways to reuse commands on Linux Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect. By Sandra Henry-Stocker Oct 15, 2024 5 mins Linux news SUSE Edge upgrade targets Kubernetes and Linux at the edge SUSE Edge 3.1 includes a new stack validation framework and an image builder tool that are aimed at improving the scalability and manageability of complex Kubernetes and Linux edge-computing deployments. By Sean Michael Kerner Oct 15, 2024 6 mins Edge Computing Linux Network Management Software how-to Lesser-known xargs command is a versatile time saver Boost your Linux command line options and simplify your work with xargs, a handy tool for a number of data manipulation tasks. By Sandra Henry Stocker Oct 11, 2024 6 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe